我有服务MyService
:
@Injectable({
providedIn: 'root'
})
在其构造函数中,我访问由myMehod()
中的方法AnotherService
填充的变量,该方法返回一个promise。
如何告诉MyService
等待myMethod()
?
我想避免:
constructor(private anotherService: AnotherService){
this.anotherService.myMethod().then(() {
//access variables here
})
}
对于模块初始化,我在myModule.module.ts
文件中做了如下操作:
...
{ provide: APP_INITIALIZER, useFactory: myProviderFactory, deps: [AnotherService], multi: true }
...
export function myProviderFactory(provider: AnotherService) {
return () => provider.myMethod();
}