在Angular 2.4.9中,以下代码对我不起作用:
export class MyComponent extends BaseComponent implements OnInit {
type = 2;
constructor() {
super();
}
ngOnInit(){
console.log('inited type', this.type)
}
}
意思是:没有控制台输出 - 无论BaseComponent
是否实现OnInit
。
如果BaseComponent
实现了钩子,那么只调用它的钩子,而不是MyComponent
上的覆盖它。
由于讨论证明在我的描述中创建的plunker确实有效,我设法在this plunkr中重现了我的问题。
有没有办法在扩展类中使用生命周期钩子?
答案 0 :(得分:0)
问题是您的type
从未设置,因此*ngIf
无效。
根据您提供的plunker,您需要提供@Input
app模板
<base-comp [type]="1"></base-comp>
<base-comp [type]="2"></base-comp>
<base-comp [type]="1"></base-comp>
<base-comp [type]="2"></base-comp>
BaseComponent字段
@Input() public type: number;