我有一个父类,我想要注入一些模块,然后我有一些派生类,我想使用这些注入的模块.
但是在派生类中,您必须调用不带参数的super(),因此未定义父类中的注入模块.
怎么可以这样做?
但是在派生类中,您必须调用不带参数的super(),因此未定义父类中的注入模块.
怎么可以这样做?
import {inject} from 'aurelia-framework';
import {HttpClient} from 'aurelia-http-client';
@inject (HttpClient)
export class Parent{
constructor(module){
//this constructor is called from derived class without parameters,//so 'module' is undefined !!
this.injectedmodule = module;
}
}
export class ClassA extends Parent{
constructor(){
super();
this.injectedmodule.get() // injectedmodule is null !!!
}
}
解决方法
好吧,刚刚找到解决方案,模块实际上是在派生类中注入并通过super()调用传递给父类:
import {inject} from 'aurelia-framework';
import {HttpClient} from 'aurelia-http-client';
@inject (HttpClient)
export class Parent{
constructor(module){
this.injectedmodule = module;
}
}
export class ClassA extends Parent{
constructor(module){
super(module);
this.injectedmodule.get() // ok !!!
}
}