为什么 Angular 注入器会检测到以下提供的内容,即使它不在注入器层次结构中?
来自官方文档,以下是轻量级注入令牌的示例 启用树摇动的模式:
abstract class LibHeaderToken {}
@Component({
selector: 'lib-header',
providers: [
{provide: LibHeaderToken, useExisting: LibHeaderComponent}
]
...,
})
class LibHeaderComponent extends LibHeaderToken {}
@Component({
selector: 'lib-card',
...,
})
class LibCardComponent {
@ContentChild(LibHeaderToken) header: LibHeaderToken|null = null;
}
我的问题是,当 Angular 看到 @ContentChild(LibHeaderToken)
时,它会尝试查找令牌名称为 LibHeaderToken
的提供程序,但是它会看起来对于当前组件或其父组件(因为它是一个分层注入器)。它不会在 LibHeaderComponent
中查找(这是我们声明所需提供程序的位置)。
那么为什么这个方法有效,为什么在寻找提供者时也会搜索 LibHeaderComponent
呢?
From the official docs, following is an example of lightweight injection token
pattern for enabling tree shaking:
abstract class LibHeaderToken {}
@Component({
selector: 'lib-header',
providers: [
{provide: LibHeaderToken, useExisting: LibHeaderComponent}
]
...,
})
class LibHeaderComponent extends LibHeaderToken {}
@Component({
selector: 'lib-card',
...,
})
class LibCardComponent {
@ContentChild(LibHeaderToken) header: LibHeaderToken|null = null;
}
My question is that when Angular sees @ContentChild(LibHeaderToken)
, it will try to look for a provider with token name as LibHeaderToken
, but it'll look for it on the current component or its parents (because its an hierarchical injector). It'll not look in LibHeaderComponent
(which is where we have declared the required provider).
So why does this work and why LibHeaderComponent
is also searched when looking for providers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您查找 @Contentchild 装饰器的定义,它是在原始文档中编写的:
在项目符号列表中,第三个项目符号表示“当前组件的子组件树中定义的任何提供程序(例如 @ContentChild(SomeService) someService: SomeService)”。因此,在示例中,LibCardHeader 位于 LibCardComponent 的子组件树中,LibHeaderToken 是在 LibCardHeader< 中定义的提供程序/em> - 这是子组件 - 与解释“当前组件的子组件树中定义的任何提供程序”匹配。当前组件是LibCardComponent。
编辑: 文档参考
If you look for the definition of @Contentchild decorator, it's written in the original docs:
In the bullet list, the 3rd bullet says "Any provider defined in the child component tree of the current component (e.g. @ContentChild(SomeService) someService: SomeService)". So in the example, LibCardHeader is in the child component tree of LibCardComponent, and the LibHeaderToken is a provider defined in the LibCardHeader -which is child component- which matches with the explanation "Any provider defined in the child component tree of the current component". The current component is LibCardComponent.
edit: docs reference