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



场景:我有两个派生类,它们都按如下方式扩展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
  }
}

支持。看看这个工作示例,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;
  }
}

相关内容

最新更新