最近,我將我的應用從 Angular 9 升級到了 12(一個一個,我沒有直接跳),當我在本地 PC 上吃午飯時,它看起來很完美:

但是當我發布并將其上傳到 GitHub Pages 時,它完全被破壞了(控制臺中沒有任何內容):

我幾乎確信該問題與與osm-map.component.ts遷移無關的組件有關。我擴展了這個組件以通過GET以下方式收集更多選項:
constructor(private service: OsmMessageServiceService, private translateService: TranslateService, private route: ActivatedRoute, private router: Router) { }
ngOnInit() {
this.router.events
.subscribe(e => {
if (e.constructor.name === 'NavigationEnd' && this.router.navigated) {
this.route.queryParams.subscribe(params => {
let isDraggable = true;
let isMetric = true;
if (params['isApp']) {
this.status = params['isApp'] == "true" ? true : false;
}
if (params['isMetric']) {
isMetric = params['isMetric'] == "true" ? true : false;
if (!isMetric) {
this.defaultUnits = 'ft/s2';
}
}
if (params['newLoc']) {
this.newLoc = params['newLoc'].split(',').map(Number);
}
localStorage.setItem('isMetric', `${isMetric}`);
localStorage.setItem('defaultUnits', this.defaultUnits);
this.resizeMap();
});
}
});
}
我什至在 V9 中發布了這些更改,并且錯誤在 GitHub Pages 中不斷發生。
由于ng-bootstrap選項卡中的某些 API 更改,我對幫助進行了細微更改,因此其余更改很小,但它們是不可見的。
這是我發布網站的方式:
ng build --prod --base-href "https://fanmixco.github.io/gravitynow-angular/"
This is a temporal website with the broken case:

I noticed that the NavigationEnd never happens because I added some logs:
ngOnInit() {
console.log("ngOnInit");
this.router.events
.subscribe(e => {
console.log("event subscriber started");
if (e.constructor.name === 'NavigationEnd' && this.router.navigated) {
console.log("NavigationEnd");

Also, if I run the compiled version on my PC, it works fine also (I must remove the GitHub URL in the index.html before). Any idea what am I doing wrong?
P.S.:
This is my repo if you want to check it:
https://github.com/FANMixco/gravitynow-angular
uj5u.com熱心網友回復:
我能夠根據這個答案修復它:https : //stackoverflow.com/a/56634968/1928691
我不得不將函式從 thengOnInit()移到 theconstructor并做一些小的調整,比如添加一個不同的 if if (event instanceof NavigationEnd)。這是最終的代碼:
constructor(private service: OsmMessageServiceService, private translateService: TranslateService, private route: ActivatedRoute, private router: Router) {
this.router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
this.route.queryParams.subscribe(params => {
完成此操作后,應用程式將按預期開始作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/335829.html
標籤:angular github github-pages angular-routing angular-router
