class ParentComponent implements OnInit{
constructor(protected router: Router){
}
ngOnInit(){
...
}
}
class ChildComponent extends ParentComponent {
constructor(protected router: Router){
super(router);
}
}
Module:
@NgModule({
declarations: [
ChildComponent
],
entryComponents: [ChildComponent]
})
Key point:
1. entryComponents必须配置
2. declarations必须配置
3. 子组件必须要有constructor而且要调用super执行父组件的constructor.
4. 如果子组件不是动态加载,那么可以不写constructor.