APP_INITIALIZER - TypeError: appInits is not a function
Asked Answered
W

3

6

I would like to initialize some important values when my application starts (Angular v17).

app.config.ts:

export const appConfig: ApplicationConfig = {
    providers: [
        ConfigService,
        ...
        {
            provide: APP_INITIALIZER,
            useFactory: (init: ConfigService) => init.load(),
            multi: true,
            deps: [ConfigService, HttpClient]
        }
    ]
};

config.service.ts:

@Injectable({
    providedIn: 'root',
})
export class ConfigService {
    private http = inject(HttpClient);
    
    private _config: any;
    private _user: AppUser;
    
    public getConfigUrl(key: string): string {
        return this._config.urls[key];
    }

    public load(): Promise<any> {
        return new Promise((resolve, reject) => {
            this._user = new AppUser(); <-- normally a request to my node-express server
            this._config = 'test';
            resolve(true);
        });
    }
}

Then I got this error when I ran the application and did not understand why.

ERROR TypeError: appInits is not a function
    at \_ApplicationInitStatus.runInitializers (core.mjs:31069:32)
    at core.mjs:34973:28
    at \_callAndReportToErrorHandler (core.mjs:31146:24)
    at core.mjs:34971:20
    at \_ZoneDelegate.invoke (zone.js:368:26)
    at Object.onInvoke (core.mjs:14424:33)
    at \_ZoneDelegate.invoke (zone.js:367:52)
    at \_Zone.run (zone.js:130:43)
    at \_NgZone.run (core.mjs:14275:28)
    at internalCreateApplication (core.mjs:34948:23)
Wurster answered 9/4, 2024 at 12:12 Comment(0)
Z
5
  1. Create a factory function as below:
export function initializeAppFactory(init: ConfigService, http: HttpClient) {
  return () => init.load();
}
  1. Provide the factory function to APP_INITIALIZER token:
export const appConfig: ApplicationConfig = {
    providers: [
        ConfigService,
        ...
        {
          provide: APP_INITIALIZER,
          useFactory: initializeAppFactory,
          multi: true,
          deps: [ConfigService, HttpClient],
        }
    ]
};

Reference: APP_INITIALIZER

Zinc answered 9/4, 2024 at 12:54 Comment(2)
Demo @ StackBlitzZinc
thank you a million times over for this answer!Regalado
F
2

A separate function isn't necessary, you just need to modify the useFactory function so that it returns a function

export const appConfig: ApplicationConfig = {
    providers: [
        ConfigService,
        ...
        {
            provide: APP_INITIALIZER,
            useFactory: (init: ConfigService) => () => init.load(),
            multi: true,
            deps: [ConfigService, HttpClient]
        }
    ]
};
Flaxman answered 22/5, 2024 at 17:49 Comment(0)
M
0

If your application is standalone You can solve it like this:

export function initializeApp(http: HttpClient) {
  return (): Promise<any> =>
    firstValueFrom(
      http
        .get("https://someUrl.com/api/user")
        .pipe(tap(user => { ... }))
    );
}
bootstrapApplication(App, {
  providers: [
    provideHttpClient(),
    {
      provide: APP_INITIALIZER,
      useFactory: initializeApp,
      multi: true,
      deps: [HttpClient],
    },
  ],
});
Magneto answered 16/10, 2024 at 9:32 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.