我有一组angular2组件,应该都会注入一些服务.我的第一个想法是,最好创建一个超级类并在那里注入服务.然后我的任何组件都会扩展该超类,但这种方法不起作用.
简化示例:
export class AbstractComponent {
constructor(private myservice: MyService) {
// Inject the service I need for all components
}
}
export MyComponent extends AbstractComponent {
constructor(private anotherService: AnotherService) {
super(); // This gives an error as super constructor needs an argument
}
}
Run Code Online (Sandbox Code Playgroud)
我可以通过MyService在每个组件中注入并使用该参数进行super()调用来解决这个问题,但这肯定是某种荒谬的.
如何正确组织我的组件,以便他们从超类继承服务?