在Angular2 tutorial通过将细节组件添加到 @NgModule
来引入细节组件。
相反,我想通过外部组件 (AppComponent
) 以某种方式导入它来添加它,以便只有外部组件引用内部组件。
我不知道该怎么做。旧示例引用 directives
属性,但 directives
不再存在于 ComponentMetadtaType
类型中。所以这是行不通的
import { HeroDetailComponent } from './hero-detail.component';
@Component({
selector: 'my-app',
[..]
directives: [HeroDetailComponent]
})
最佳答案
您必须向模块的 declarations: []
添加指令和组件。
如果您希望只有一个组件能够使用一个组件,请创建一个仅由这两个组件组成的模块。
@NgModule({
imports: [BrowserModule],
declarations: [AppComponent, FooComponent, BarDirective],
...
})
关于angular - 如何在 Angular2 中声明嵌套组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39559227/