角单例服务初始化等待承诺

时间:2019-11-06 16:10:26

标签: angular

我有服务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();
}

0 个答案:

没有答案