【发布时间】:2016-05-06 01:54:27
【问题描述】:
情况:
我有一个基类Base。 Main 扩展 Base。 Main 通过super() 将服务传递给Base。 Base 然后调用该服务的方法。
问题:
服务是Base 中的undefined。除非我 console.log(_service) 先于基类。我怀疑与通过 AsyncRoute 延迟加载有关的竞争条件,或者它可能是一个实例化问题......无论哪种方式,我都不知道如何开始纠正它。
Main.ts:
export class Main extends Base {
constructor(private _service:SomeService) {
super(this._service);
}
}
Base.ts:
@Injectable()
export class Base {
constructor(_service) {
_service.doSomething(true);
}
}
代码在_service.doSomething(true) 出现错误:
异常:Main 实例化期间出错!
原始异常:TypeError:无法读取未定义的属性“doSomething”
但是,如果我尝试追踪为什么 _service 未定义执行以下操作 - 它神奇地起作用???
Base.ts:
@Injectable()
export class Base {
constructor(_service) {
console.log("_service?", _service); // if this is here...
_service.doSomething(true); // ...then the error no longer occurs here
}
}
【问题讨论】:
标签: typescript angular angular2-services