简体   繁体   English

使用 ng-template 的 ContentChildren 在组件内生成 ExpressionChangedAfterItHasBeenCheckedError

[英]ContentChildren with ng-template generates ExpressionChangedAfterItHasBeenCheckedError inside component

I am working on an Angular application, and I've faced this ExpressionChangedAfterItHasBeenCheckedError a few times, but this time I don't know what to do to solve it.我正在开发一个 Angular 应用程序,我遇到过几次ExpressionChangedAfterItHasBeenCheckedError ,但这次我不知道该怎么解决它。

I have a component that has a few child components using @ContentChild and @ContentChildren .我有一个组件,它有几个使用@ContentChild@ContentChildren的子组件。 Inside the child component, there is a list of children, all of which I'm rendering using *ngTemplateOutlet .在子组件内部,有一个子组件列表,我使用*ngTemplateOutlet渲染所有这些子组件。

I've recreated the situation with an example based on a TableComponent that has a HeaderComponent , which can have multiple HeaderItemComponent inside, and a QueryList of RowItem .我已经用一个基于TableComponent的示例重新创建了这种情况,该示例具有一个HeaderComponent ,其中可以有多个HeaderItemComponent ,以及一个QueryListRowItem

I've made a StackBlitz with the full code to reproduce the error at:我用完整的代码制作了一个 StackBlitz 来重现错误:

https://stackblitz.com/edit/angular-6ieqh7 https://stackblitz.com/edit/angular-6ieqh7

The weird thing to me is that, if I remove the header when rendering the table, no errors occur, even though the header items are rendered the exact same way as the rows.对我来说奇怪的是,如果我在呈现表格时删除 header,则不会发生错误,即使 header 项目的呈现方式与行完全相同。

Error occurs:发生错误:

<table>
  <table-header>
    <table-header-item>Id</table-header-item>
    <table-header-item>Name</table-header-item>
    <table-header-item>Status</table-header-item>
  </table-header>

  <table-row *ngFor="let row of tableData">
    <table-row-item>{{ row.id }}</table-row-item>
    <table-row-item>{{ row.name }}</table-row-item>
    <table-row-item>{{ row.status }}</table-row-item>
  </table-row>
</table>

No errors:没有错误:

<table>
  <table-row *ngFor="let row of tableData">
    <table-row-item>{{ row.id }}</table-row-item>
    <table-row-item>{{ row.name }}</table-row-item>
    <table-row-item>{{ row.status }}</table-row-item>
  </table-row>
</table>

Finally, the error, accused in line 2 of header.component.html :最后,错误,在header.component.html的第 2 行指责:

ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngTemplateOutlet: undefined'. Current value: 'ngTemplateOutlet: [object Object]'.

You can fix that error by wrapping your headers item inside setTimeOut您可以通过将标题项包装在 setTimeOut 中来修复该错误

For More Info About Check this: 有关检查的更多信息:

ngAfterContentInit(): void {
    setTimeout(()=>{
         this.headerItems = this.inputHeaderItems.toArray();
        this.inputHeaderItems.changes.subscribe((value: QueryList<HeaderItemComponent>) => {
          this.headerItems = value.toArray();
          this.cdr.detectChanges();
        });
      })

      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM