在阅读了与变更检测和类似帖子相关的所有材料并未能解决我的问题后,我在这里发帖。
变化检测器参考detectChanges()
从订阅内部调用时会导致无限循环。如果我不打电话detectChanges
, I get ExpressionChangedAfterCheck
错误。我真的不知道如何解决,为什么ExpressionChangedAfterCheck
错误来了
ngAfterViewInit() {
this.route.params.subscribe((params) => {
this.userId = params.userId;
if (this.userId != undefined) {
this.loadActivity();
}
else {
alert("Userid not supplied");
}
this.cdRef.detectChanges();
});
}
loadActivity() {
while(this.layers.length > 0){
this.layers.pop();
}
this.loading = true;
this.userService.authenticatedGet(ApiUrls.GetLocation(this.userId, this.activity.from.getTime(), this.activity.to.getTime()), {}).subscribe(
(res:any) => {
this.user = res.user;
this.loading = false;
// other logic
this.cdRef.detectChanges();
console.log("Loading is " + this.loading);
}
);
}
注意:只有当值与 绑定时才会出现此问题ngIf
.
我解决了这个问题。问题不在于生命周期,而在于指令leaflet
来自 ngx-leaflet 项目。
当我删除与传单相关的指令和绑定时,所有错误都消失了。
即使这样也出现了错误:
<ng-container *ngIf="!loading">
<div
class="map"
leaflet
(leafletMapReady)="onMapReady($event)"
[leafletOptions]="options"
[leafletLayers]="layers"
[leafletLayersControl]="layersControl">
</div>
</ng-container>
我尝试交替添加 detectorChanges 和 markForCheck,但再次没有运气。
onMapReady(map: Map) {
this.map = map;
setTimeout(() => map.invalidateSize(), 1000);
this.route.params.subscribe(params => {
this.userId = params["userId"];
this.loadActivity();
});
this.cdRef.markForCheck();
}
最后,我在写这个答案时放弃了传单,我将尝试 Angular-Google-Maps。
是的,A-G-M 工作得很好。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)