简体   繁体   中英

How to call a service in angular APP_Initializer?

I want to load api data before app onInit(). I am not able to inject the service dependency.

How I can inject that?

 import { GetComissionService } from '../app/services/get-comission.service'; function initializeApp(comissionService: GetCommissionService): Promise<void> { return new Promise((resolve, reject) => { GetComissionService.getCommission('item').subscribe(data => { console.log(data); resolve(); }) }); } @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, HttpClientModule ], providers: [{ provide: APP_INITIALIZER, deps: [GetComissionService], useFactory: () => initializeApp, multi: true }], bootstrap: [AppComponent] })

EDIT 1: adding error message images

Getting these errors while injecting service

在此处输入图像描述 在此处输入图像描述

You also need to have a parameter on the initializeApp function for the service. Try this:

export function initializeApp(comissionService: GetCommissionService): Promise<any> {
  return new Promise((resolve, reject) => {
    comissionService.getCommission('item').subscribe(data => {
      console.log(data);
      resolve();
     })
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM