虽然我知道发生这种情况的原因ERROR
,但是,我想知道如何在以下情况下克服:
我有一个通用加载器app.component
。对于某些子路由,它会在ngOnInit
生命周期中更新加载器值,直到获取记录。另外,我不想使用resolver
,因为我正在解析组件中的查询参数,然后传递给服务以获取记录。
这ERROR
是由于在子组件生命周期钩子期间加载器的默认值false
和更改。true
ngOnInit
请查看以下代码段:
// app.component.ts
viewModel = {
showLoader: false
};
ngOnInit() {
this.eventService.on(ApplicationEvents.SHOW_FULL_PAGE_LOADER, (value) => {
this.viewModel.showLoader = value;
});
}
// app.component.html
<div class="full-page-loader" *ngIf="viewModel.showLoader"><em class="fa fa-cog fa-spin loader-icon"></em></div>
以下是延迟加载子组件的片段:
ngOnInit() {
this.loadData();
this.cart = this.cartService.getCart();
}
private loadData() {
this.eventService.showFullPageLoader(); // <-- This will emit an event to show the loader
this.umrahDataService.getServices(UmrahServiceType.Visa).then((resp) => {
this.visas = resp.map((v) => {
v.image = '/assets/img/umrah/visa-processing.jpg';
return v;
});
this.eventService.hideFullPageLoader();
this.actionInProcess = false;
}).catch((er) => {
this.alertService.alert('SERVER_ERROR');
this.eventService.hideFullPageLoader();
this.actionInProcess = false;
});
}
ERROR 错误:ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。以前的值:'ngIf: false'。当前值:'ngIf: true'。在 viewDebugError (core.js:19629) 在 expressionChangedAfterItHasBeenCheckedError (core.js:19617) 在 checkBindingNoChanges (core.js:19833) 在 checkNoChangesNodeInline (core.js:29533) 在 checkNoChangesNode (core.js:29522) 在 debugCheckNoChangesNode (core. js:30126) 在 debugCheckDirectivesFn (core.js:30054) 在 Object.eval [as updateDirectives] (AppComponent.html:3) 在 Object.debugUpdateDirectives [as updateDirectives] (core.js:30043) 在 checkNoChangesView (core.js: 29421)
尽管我也尝试过BehaviorSubject
使用async
管道,但这也无济于事。期待反馈。谢谢