[英]Injecting parent component using class that parent component extends
我有多个不同的角度分量。 在这些组件中,还有一个基本列表组件。
例如:VegetablesComponents具有BaseListComponent,FruitsComponent具有BaseListComponent等...当然,Vegetables / Fruits组件内部也有不同的子组件。
BaseListComponent从父组件(VegetablesComponent,FruitsComponent)获取一些修改某些功能的数据。 这是通过在BaseListComponent上使用@Input()完成的。
因此,我想到了VegetablesComponent,FruitsComponent可以扩展同一类。 而且我可以将父组件注入BaseListComponent内。 可以说:
VegetablesComponents extends HasBaseFunctionality {}
然后在BaseListComponent内部,我将使用构造函数(父级:HasBaseFunctionality,...)。
这将减少BaseList组件上所需的输入数量,并使它看起来更加整洁。
但是一旦我尝试这样做,我就会得到:
StaticInjectorError(AppModule)[ChildComponentComponent ->
BaseComponentComponent]:
StaticInjectorError(Platform: core)[ChildComponentComponent ->
BaseComponentComponent]:
NullInjectorError: No provider for BaseComponentComponent!
如果我要使用:
constructor(parent: VegetablesComponent) {...}
会的。
因此,我想知道是否有任何方法可以在不使用服务的情况下使用该服务,一旦初始化,我将在该服务上注册当前基本组件,然后使用服务访问其值。
您的每个子组件都需要提供父类:
@Component({
providers: [
{
provide: BaseComponentComponent,
useExisting: VegetablesComponent
}
]
})
然后,您通常可以注入BaseComponentComponent
类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.