我试图将Injector
服务注入自定义回调中,以便该回调可以使用它来实例化其自身的依赖关系。像这样:
labelObservable: (injector: Injector) => {
let myCustomService = injector.get<CustomService>(CustomService);
return myCustomService.getSomethingFromTheAPI();
}
但是,injector
服务在我的回调中未定义,但在我调用此回调时却未定义
export class SidebarComponent implements OnInit {
constructor(public injector: Injector) {
}
ngOnInit() {
ROUTES.filter(value => value.labelObservable).map(value => {
(value.labelObservable.call(this.injector)).subscribe(observableValue => {
/* doing something with the observable value */
})
});
/* Irrelvent init code */
}
这很烦人,因为当我注入具有HTTPClient
属性的服务并“手动”执行请求时,它正在工作。
有什么我想念的东西吗,也许我处于摘要循环中,需要等待,我不知道。
任何帮助将不胜感激。
答案 0 :(得分:0)
我丢失了一些东西,它与Injector服务本身无关。
call
函数有两个参数,但是,我只发送了一个。
因此,args
参数为空。这导致未定义回调参数。