当 TemplatePortal 附加到父 PortalOutlet 时,ExpressionChangedAfterItHasBeenCheckedError 在更改检测时抛出

ExpressionChangedAfterItHasBeenCheckedError thrown on change detection when TemplatePortal attached to parents PortalOutlet

我有以下组件结构:App -> GrandParent -> Parent -> Child

  1. ChildComponennt 在其模板中声明了一个 <ng-template cdkPortal>
  2. AppComponent、GrandParentComponent 和 ParentComponent 各自在各自的模板中声明了一个 <ng-template cdkPortalOutput>

现在,如果 ChildComponent 附加其门户 属性:

Stackblitz:https://stackblitz.com/edit/portal-cdk?file=src%2Fapp%2Fchild%2Fchild.component.ts(取消注释 ChildComponent#ngOnInit 中的调用)

有人对此行为有解释吗?使用 CDK 的门户在位于上面几个组件的 PortalOutlet 中显示 TemplatePortal 的正确方法是什么?

谢谢!

我无法找到您问题的解决方案,如果有人知道,我也很感兴趣,但至少我为您创建了解决方法。它不使用 cdk-portal,仅使用 Angular 的 viewContainerRef,但我相信 cdk-portal 使用类似的方法。

堆栈闪电战:https://stackblitz.com/edit/portal-cdk-uiyxhn (取消注释 ChildComponent#ngOnInit 中的调用)