【发布时间】:2019-03-19 11:25:50
【问题描述】:
我正在为我的一个项目实施动态组件。动态组件的概念是一旦需要它们就会进入内存,并且在任何模板中都没有引用。
根据官方docs的说法,我们在entryComponents中声明了这些组件,以防止它们在摇树过程中被丢弃,因为它们没有模板引用。
下面是app.module.ts,我在entryComponents数组中声明了我的两个动态组件SlideOneComponent和SlideTwoComponent:
@NgModule({
declarations: [
AppComponent,
ButtonComponent,
AdDirective
],
imports: [
BrowserModule
],
providers: [],
entryComponents: [
SlideOneComponent,
SlideTwoComponent,
],
bootstrap: [AppComponent]
})
export class AppModule { }
上面app.module.ts我收到以下错误:
只要将两个动态组件都添加到declarations 数组,上述错误就会消失。上述官方文档还说,我们不需要声明可从entryComponents 或bootstrap 组件访问的组件。我也访问了this 答案,但这似乎不够令人满意,因为它是关于 Ionic 的。
请帮助我知道我在这方面缺少什么。提前致谢! :)
【问题讨论】:
-
The aforementioned official docs also says that we do not need to declare components that are reachable from entryComponents or bootstrap component.你能指出文档说明这一点的确切位置吗? -
您必须在
declarations数组中声明任何组件。除此之外,入口组件还必须在entryComponents数组(NgModule 或组件级别)中显式或通过bootstrap数组或路由声明
标签: javascript angular angular-dynamic-components