我正在尝试动态创建标头,构造标头模板并将其作为“字符串”传递给我的动态组件。我的要求是在标头组件模板字符串中调用另一个组件并加载它。
已经创建了一个工作Plunker https://plnkr.co/edit/ENa23Q1Qqtvi46CYnllV?p=preview这里是场景。
我在浏览器控制台中收到以下错误:
Unhandled Promise rejection: Template parse errors:
'alert' is not a known element:
1. If 'alert' is an Angular component, then verify that it is part of this module.
2. If 'alert' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. (" <span class="title" style="float: right; margin-right: 10px;">
[ERROR ->]<alert></alert>
<img class="notify-circle" src="https://cdn0.iconf"): DynamicComponent@2:32 ; Zone: <root> ; Task: Promise.then ; Value: Object { _nativeError: Error, stack: "" } BaseError@https://unpkg.com/@angular/compiler/bundles/compiler.umd.js:1595:29 [angular]
问题:
如何使用选择器在构造为字符串的标头模板内识别“警报”组件'<alert></alert>'
.
这个概念可能看起来很奇怪,但要求就是这样。
您可以创建特殊的共享模块,其中始终包含您需要的内容
@NgModule({
declarations: [AlertComponent],
exports: [AlertComponent]
})
export class SharedDynamicModule {}
然后将其导入到您的DynamicModule
动态.ts
@NgModule({
imports: [ CommonModule, SharedDynamicModule ],
declarations: [ DynamicComponent ]
})
class DynamicHtmlModule { }
改良版普鲁克 https://plnkr.co/edit/j3eiVTUayxbzAyqmSlSz?p=preview
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)