如何修复 Angular 'TypeError: templateRef.createEmbeddedView is not a function'
问题:
你遇到类似这样的错误消息:
error_trace.txt
ERROR TypeError: templateRef.createEmbeddedView is not a function
at ViewContainerRef_.createEmbeddedView (core.js:11389)
at NgIf._updateView (common.js:2843)
at NgIf.set [as ngIfElse] (common.js:2815)
at updateProp (core.js:12602)
at checkAndUpdateDirectiveInline (core.js:12313)
at checkAndUpdateNodeInline (core.js:13876)
at checkAndUpdateNode (core.js:13819)
at debugCheckAndUpdateNode (core.js:14712)
at debugCheckDirectivesFn (core.js:14653)
at Object.eval [as updateDirectives] (MyComponent.html:1)在一个源代码类似这样的组件中
ngIf_else_broken.html
<div *ngIf="myCondition ; else elseSection">
<!-- ... -->
</div>
<div #elseSection>
<!-- ... -->
</div>解决方案
你在 *ngIf else 子句中引用的任何元素不能是任意组件,它必须是 ng-template。
要解决此问题,将 <div #elseSection> 更改为 <ng-template #elseSection>。注意仅使用 <template> 自 Angular4 起已弃用。
结果源代码应如下所示:
ngIf_else_fixed.html
<div *ngIf="myCondition ; else elseSection">
<!-- ... -->
</div>
<ng-template #elseSection>
<!-- ... -->
</ng-template>Check out similar posts by category:
Angular, Javascript
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow