2016-04-15 3 views
1

シナリオ:次のようにActionBaseクラスを拡張する2つの派生クラスがあります。両方の派生クラスにDIを使いたいです。しかし、両方のクラスには異なる依存関係があります。それは可能でしょうか?だから私は何が間違っているの?どちらの場合も、注入されたインスタンス/モジュールは「未定義」です。任意のヘルプ/ヒントが高く評価されました。Aurelia:Dep。派生クラスへの注入は不可能ですか? (または何が間違っているのですか?)

/* 
* 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 
    } 
} 
+0

。 '。/ ActionBase'がActionBaseクラスファイルの正しいパスであると確信していますか? –

+0

はい。その部分はうまく動作します(上記のスニペットはもちろん単一のファイルではありません)。メソッドは継承され、期待どおりに動作します...基本クラスレベルに依存関係を注入する場合にのみ機能しますが、すべての派生クラスに同じ依存関係があるわけではないので、オプションではありません。何か案は? – simonwidjaja

答えて

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; 
    } 
} 
関連する問題