[英]Use case for private components
I'm trying to come up with a simple use case for private components and here it it. 我正在尝试为私有组件提出一个简单的用例,并在这里找到它。 Suppose I have the module
HelloWorldAppModule
with one public and one private 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 {
}
As you can see, this module exports only HelloWorldComponent
, however, inside the template of HelloWorldComponent
the PrivateComponent
is used. 如您所见,此模块仅导出
HelloWorldComponent
,但是,在HelloWorldComponent
模板内部使用PrivateComponent
。 This should work fine, since both components are registered in declarations
. 由于两个组件都在
declarations
中注册,因此应该可以正常工作。
Then, I create another module UsesHelloWorldModule
that imports HelloWorldAppModule
and so, as I understand, I can use components exported by it in templates of directives registered within HelloWorldAppModule
. 然后,我创建另一个模块
UsesHelloWorldModule
,该模块导入HelloWorldAppModule
,因此,据我了解,我可以在HelloWorldAppModule
注册的指令模板中使用由其导出的组件。 So here it is: 所以这里是:
@Component({
selector: 'uses-hello-world',
template: `<hello-world></hello-world><private></private>`
})
class UsesHelloWorldComponent {
}
@NgModule({
imports: [HelloWorldAppModule],
declarations: [UsesHelloWorldComponent],
})
class UsesHelloWorldModule {
}
However, I also used <private>
component from the HelloWorldAppModule
that was not exported. 但是,我还使用了未导出的
HelloWorldAppModule
中的<private>
组件。 So what's going to happen? 那么会发生什么呢? Am I right that angular is going to throw an error when parsing the
<private></private>
tag? 我是否正确,在解析
<private></private>
标记时angular会抛出错误?
EDIT: 编辑:
Also, what will happen if only is used inside UsesHelloWorldComponent
: 另外,如果仅在
UsesHelloWorldComponent
内部使用,将会发生什么:
@Component({
selector: 'uses-hello-world',
template: `<hello-world></hello-world>`
})
class UsesHelloWorldComponent {
}
Please note that it uses <private>
inside its template. 请注意,它在模板内使用
<private>
。
Appreciate any comments as there is not much examples of private components on the web. 感谢任何评论,因为网络上没有太多私有组件的示例。
<private></private>
should cause an error about missing CUSTOM_ELEMENTS_SCHEMA
. <private></private>
应该导致有关缺少CUSTOM_ELEMENTS_SCHEMA
的错误。 If you proveded CUSTOM_ELEMENTS_SCHEMA
to be able to use custom tags that are not Angular components, then you won't get an error, unless you also add bindings like <private [prop]="value">
- which will cause an error because <private>
won't have a prop
property when it's not a PrivateComponent
如果您证明
CUSTOM_ELEMENTS_SCHEMA
能够使用不是Angular组件的自定义标签,那么您将不会得到错误,除非您还添加了<private [prop]="value">
类的绑定,这将导致错误,因为<private>
不是PrivateComponent
则不会具有prop
属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.