首页 > 解决方案 > 我们如何使用 TypeScript mixin 和依赖注入?

问题描述

当我使用mixins进行多重继承时,如果在我继承的任何类中都有依赖注入,我该如何使用super()?

代码示例:

混音:

export const applyMixins = (derivedCtor: any, baseCtors: any[]) => {
  baseCtors.forEach(baseCtor => {
    Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
      Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name));
    });
  });
};

AsyncComponent 构造函数:

export class AsyncComponent {
  constructor(private communication: CommunicationsService) {}
}

界面:

export interface DashboardPageComponent extends AsyncComponent {}

构造函数:

export class DashboardPageComponent {

constructor(private myPropertiesService: MyPropertiesService, private state: StateService) {
    super()
  }
}

我收到以下错误:

'super' 只能在派生类中引用

另一方面,如果我通过依赖注入继承了多个类,我该如何使用 super()?

标签: angulartypescriptmixins

解决方案


推荐阅读