Angular 专用组件的用例
我试图为私有组件提供一个简单的用例,这里就是它。假设我有一个模块Angular 专用组件的用例,angular,angular2-modules,Angular,Angular2 Modules,我试图为私有组件提供一个简单的用例,这里就是它。假设我有一个模块HelloWorldAppModule,其中包含一个公共组件和一个私有组件: @Component({ selector: 'hello-world', template: ` <div> <private></private> </div>` }) class HelloWorldComponent { } @Component({
HelloWorldAppModule
,其中包含一个公共组件和一个私有组件:
@Component({
selector: 'hello-world',
template: `
<div>
<private></private>
</div>`
})
class HelloWorldComponent {
}
@Component({
selector: 'private',
template: `<span>I am private</span>`
})
class PrivateComponent {
}
@NgModule({
declarations: [HelloWorldComponent, PrivateComponent],
exports: [HelloWorldComponent]
})
class HelloWorldAppModule {
}
但是,我还使用了未导出的HelloWorldAppModule
中的
组件。那么会发生什么呢?angular在解析
标记时会抛出错误,这对吗
编辑:
此外,如果仅在UsesHelloWorldComponent
中使用,会发生什么情况:
@Component({
selector: 'uses-hello-world',
template: `<hello-world></hello-world>`
})
class UsesHelloWorldComponent {
}
@组件({
选择器:“使用hello world”,
模板:``
})
类UsesHelloWorldComponent{
}
请注意,它在模板中使用
感谢您的评论,因为web上没有太多的私有组件示例。
应该会导致关于缺少自定义元素\u架构的错误。如果您证明CUSTOM\u ELEMENTS\u SCHEMA
能够使用非角度组件的自定义标记,则不会出现错误,除非您还添加了像
-这样的绑定,这将导致错误,因为
在它不是私有组件时不会有prop
属性是的,它会抛出一个找不到组件的异常。谢谢,如果我没有在UsesHelloWorldComponent
模板中使用
,
组件仍将在其模板内以
呈现,没有错误,对吗?是的,但仅作为自定义HTML元素,而不是角度组件(我希望我正确理解您的注释),可能我没有清楚地说明问题。如果查看UsesHelloWorldComponent
模板,您可以看到
在模板中使用。这应该可以正常工作,因为HelloWorldAppModule
导出HelloWorldComponent
。但是,在
内部使用了
组件,该组件未导出。那会发生什么?我对问题进行了编辑,以表明我仍然难以理解你的观点。如果在
内部使用,则将生成
元素的组件,无论您在何处使用导出的组件(在声明组件的模块内部或外部),其内容将始终相同。谢谢,基本上,在模块中定义但未导出的组件只能在该模块中定义的组件模板中使用,对吗?
@Component({
selector: 'uses-hello-world',
template: `<hello-world></hello-world>`
})
class UsesHelloWorldComponent {
}