Aurelia:Dep。注入派生类是不可能的? (或者我做错了什么?!)

时间:2016-04-15 20:00:44

标签: aurelia systemjs

场景:我有两个派生类,它们都扩展了ActionBase类,如下所示。我想对两个派生类使用DI。但是这两个类都有不同的依赖关系。这应该是可能的,对吧?那么我做错了什么?在这两种情况下,注入的实例/模块都是未定义的'。任何帮助/提示都表示赞赏。

/*
 * Base class for Actions
 */

export class ActionBase {

  type;

  constructor(type) {
    this.type = type;
  }
}





/*
 * Derived Class: InsertAction
 */

import {inject} from 'aurelia-framework';
import {ActionBase} from './ActionBase';
import {PomManager} from '../manager/PomManager';

@inject(PomManager)
export class InsertAction extends ActionBase {

  pomManager;

  constructor(pomManager) {
    super("insert");
    this.pomManager = pomManager;
    console.log("[InsertAction:constructor] pomManager: ", this.pomManager); // undefined
  }
}





/*
 * Derived Class: RenderAction
 */

import {inject} from 'aurelia-framework';
import {ActionBase} from './ActionBase';
import {AnotherManager} from '../manager/AnotherManager';

@inject(AnotherManager)
export class RenderAction extends ActionBase {

  anotherManager;

  constructor(anotherManager) {
    super("render");
    this.anotherManager = anotherManager;
    console.log("[RenderAction:constructor] anotherManager: ", this.anotherManager); // undefined
  }
}

1 个答案:

答案 0 :(得分:1)

支持。看看这个工作示例,其中Action1和Action2扩展了BaseAction,每个都采用不同的依赖关系。

以下是一个例子:https://gist.run?id=0efabf77c649f41981dcde753fdc542c

<强> app.js

import {inject} from 'aurelia-dependency-injection'
import {Action1, Action2} from './classes'

@inject(Action1, Action2)
export class App {
  constructor(a1, a2){
    this.message = "look at console output";
    console.log("a1",  a1.dep.constructor.name);
    console.log("a2",  a2.dep.constructor.name); 
  }
}

<强> classes.js

import {inject} from 'aurelia-dependency-injection'
export class Action1Dependency {}
export class Action2Dependency {}

export class ActionBase{

}

@inject(Action1Dependency)
export class Action1 extends ActionBase{
  constructor(dep){
    super();
    this.dep = dep;
  }
}

@inject(Action2Dependency)
export class Action2 extends ActionBase{
  constructor(dep){
    super();
    this.dep = dep;
  }
}