我最近将我的 Angular 项目更新到版本 9。我正在使用一项服务来更新主菜单的标题,它运行成功,但现在生成了这个错误:
core.js:5828 ERROR 错误:ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。以前的值:'主标题'。当前值:“新标题”。
每次标题更改时都会发生此错误。
这是我的代码的一部分:
我的路由:
应用程序路由.module.ts:
const routes: Routes = [
{
path: '',
component: MainComponent,
canActivate: [AuthGuard],
canActivateChild: [AuthGuard],
children: [
{
path: '',
component: DashboardComponent
},
{
path: 'warehouse',
loadChildren: () =>
import('./systems/warehouse/warehouse.module').then(
m => m.WarehouseModule
)
},
{
path: 'commerce',
loadChildren: () =>
import('./systems/commerce/commerce.module').then(
m => m.CommerceModule
)
},
{
...
主要成分:
main.component.ts:
pageTitle: string;
ngOnInit() {
this.mainService.pageTitle$.subscribe((title: string) => {
this.pageTitle = title;
});
...
我用来更改标题的服务: main.service.ts:
public setTitle(title: string) {
this.pageTitle$.emit(title);
}
最后是我在仓库模块中更改标题的客户之一:
main.service.ts:
this.mainService.setTitle('new title');
更新:
该问题仅在启用 Ivy 并处于开发模式时发生。"enableIvy": false或生产时没有错误。