Angular 不同类型的孩子
我有一个组件(单选组),它将其他组件(单选按钮)分组 要获取组中包含的所有按钮,请执行以下操作:Angular 不同类型的孩子,angular,typescript,Angular,Typescript,我有一个组件(单选组),它将其他组件(单选按钮)分组 要获取组中包含的所有按钮,请执行以下操作: @ContentChildren(RadioButtonComponent,{子体:true}) 无线电:QueryList; radioButton是一个正常的角度组件 @组件({ 选择器:“应用程序单选按钮”, templateUrl:'./单选按钮.component.html', 样式URL:['./单选按钮.component.scss'], }) 导出类RadioButton组件实现无
@ContentChildren(RadioButtonComponent,{子体:true})
无线电:QueryList;
radioButton是一个正常的角度组件
@组件({
选择器:“应用程序单选按钮”,
templateUrl:'./单选按钮.component.html',
样式URL:['./单选按钮.component.scss'],
})
导出类RadioButton组件实现无线电{
@Input()值:任意;
@Input()已禁用:布尔值;
//等等。。。
}
现在,我想用不同的模板创建一个新类型的单选按钮,但代码与radioButton for ex相同
@组件({
选择器:“应用程序单选平铺按钮”,
templateUrl:'./单选平铺按钮.component.html',
styleUrls:['./单选平铺按钮.component.scss'],
})
导出类RadioTiledButtonComponent实现无线电{
@Input()值:任意;
@Input()图像:字符串;
@Input()名称:string;
@Input()已禁用:布尔值;
//等等。。。
}
问题是,尽管它们共享相同的公共方法和接口,但我没有找到在ContentChildren中恢复它们的方法
@ContentChildren(RadioButtonComponent,{子体:true})
无线电:QueryList;
我该怎么做
我看过这个帖子=>
但它没有回答我的问题,因为指令没有模板,所以这是一个不同的问题。如果它们共享相同的公共方法和接口,您总是可以使用抽象类执行contentchildren 在两个组件元数据中,执行以下操作:
@Component({
selector: 'ComponentA',
provider: [{ provide: IParentComponent, useExisting: forwardRef(() => ComponentA) }],
template: '',
})
export class ComponentA implements IParentComponent {
getSelectedValue(): { return 1; }
}
@Component({
selector: 'ComponentB',
provider: [{ provide: IParentComponent, useExisting: forwardRef(() => ComponentB) }],
template: '',
})
export class ComponentB implements IParentComponent {
getSelectedValue(): { return 1; }
}
// some example methods
export abstract class IParentComponent {
abstract getSelectedValue(): number;
}
其次是:
@ContentChildren(IParentComponent,{子体:true})
无线电:QueryList
这将为您提供两个组件的句柄。IParentComponent必须是组件,对吗?不仅仅是一个接口?iPart是一个抽象类,它不需要是一个组件。在类中,可以添加共享方法的定义。检查更新的代码。在提供程序中,在内容子级中,它说,
'IParentComponent'仅指类型,但在此处用作值。
ok Abstract class make more Sense此错误应随Abstract class一起消失