Angular Async Factory Provider

前端 未结 2 2189
故里飘歌
故里飘歌 2021-02-19 20:11

I would like to set up a factory that does async work to return a service, and then provide that factory to a factory provider to provide that service to a component when it loa

2条回答
  •  忘了有多久
    2021-02-19 20:41

    you can make your promise function to be async

    export function testFactory(auth: any, temp: any) {
      return new Promise(async(res, rej) => {
        const inst = new TestService();
        await inst.ngOnInit();
        res(inst);
      });
    }
    
    export let testProvider =
    {
      provide: TestService,
      useFactory: testFactory,
      deps: []
    };
    

提交回复
热议问题