我有一个组件,它充当许多组件的蓝图。他们都共享相同的方法。我怎样才能从中制定蓝图?
我不想重复所有其他组件中的方法。如果我需要更改某些功能,我只想重写一个方法。
@Component({
template: `
<datagrid (onRefresh)="loadCollection($event)" (onRowSelect)="loadRecord($event)" (onCreateRecord)="createRecord()"></datagrid>
`,
providers: [CollectionService]
})
export class ListComponent implements OnInit{
loadCollection($event){
...
}
loadRecord($event){
...
}
createRecord(){
...
}
}
最佳答案
尝试下面,
export class BaseComponent {
name: string ="";
someCommonFunction(){
return `Method called from Base Component from child ${this.name}`
}
}
@Component({
selector: 'child-1',
template: `{{someCommonFunction()}}`
})
export class ChildComponent1 extends BaseComponent {
constructor(){
super();
this.name = "ChildComponent1";
}
}
@Component({
selector: 'child-2',
template: `{{someCommonFunction()}}`
})
export class ChildComponent2 extends BaseComponent {
constructor(){
super();
this.name = "ChildComponent2";
}
}
希望这有帮助!!
关于Angular - 组件蓝图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42651583/