我有三个部件。一个父组件,它可以将任意两个子组件中的一个封装在ng-内容中。我的子组件具有共享功能,因此它们正在扩展具有共享功能的抽象类。
当我尝试使用ContentChildren获取ng内容引用时,如果我使用的是其中一个子类,它就可以正常工作。当我引用抽象类时,它不起作用。有办法让这件事成功吗?http://plnkr.co/edit/f3vc2t2gjtOrusfeesHa?p=preview是:
这是我的代码:
儿童抽象课程:
abstract export class Child{
value: any;
abstract setValue();
} 我的父代码:
@Component({
selector: 'parent',
template: `
<div>
parent
<ng-content></ng-content>
</div>
`,
})
export class Parent {
@ContentChild(Child)
child: Child;
name:string;
constructor() {
}
ngAfterViewInit() {
this.child.setValue();
}
}第一个孩子:
@Component({
selector: 'child1',
template: `
<div>
value is: {{value}}
</div>
`,
})
export class Child1 extends Child {
name:string;
constructor() {
}
setValue() {this.value = 'Value from child 1'}
}第二个孩子:
@Component({
selector: 'child2',
template: `
<div>
value is: {{value}}
</div>
`,
})
export class Child2 extends Child {
name:string;
constructor() {
}
setValue() {this.value = 'Value from child 2'}
}发布于 2017-05-11 23:57:50
我用以下方法解决了这个问题:
到我的Child1组件装饰器中
providers: [{provide: Child, useExisting: forwardRef(() => Child1)}],到我的Child2组件装饰器中
providers: [{provide: Child, useExisting: forwardRef(() => Child2)}],现在,您可以从角来查看新的io示例:
https://stackoverflow.com/questions/43926916
复制相似问题