我是A2的新手,所以这个问题可能以前曾被问过,但是我使用了错误的术语,所以找不到答案。Angular2 - NgModule外部的组件注入
但是,我想了解A2内的@Component
注射。尽我所知,所有的组件不管如何嵌套的,他们总是有作为NgModules文件的一部分在根级别声明(在这里声明即服务都可以访问通过所有部件)
@NgModule({
imports: [
...
],
declarations: [
1stComponent,
2ndComponent,
3rdComponent,
4thComponent <-- All components must be included here
],
providers: [
...
],
bootstrap: [
AppComponent
]
})
对我来说,这似乎有点奇怪。如果您有一个ChildComponent
,只会显示为ParentComponent
的一部分,该怎么办?为什么ChildComponent
需要在NgModule
级别声明?在需要时,这个ChildComponent
不能被声明为父组件的一部分吗?在加载应用程序时,预先声明所有组件是否会导致开销 - 它需要将所有内容都提前?还是仅仅是NgModule
定义了构建组件的内容,而不是在运行时显示它们的时间或方式?