angularangular19

angular 19 - APP_INITIALIZER deprecation


I am trying to upgrade from Angular 18 to 19. Automatic migration changed the following code

 providers: [
    { 
      provide: APP_INITIALIZER, 
      useFactory: initializeApp1, 
      deps: [AuthService], 
      multi: true 
    },
  ]

to this:

 providers: [
  provideAppInitializer(initializeApp1(inject(AuthService)))
 ]

however, when I run the code, I am getting this error

Uncaught RuntimeError: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with runInInjectionContext. Find more at https://angular.dev/errors/NG0203`

I am pretty stuck as to how to solve this error, could you help me?


Solution

  • You'll need to rely on an arrow function to be in the injection context:

    provideAppInitializer(() => intializeApp1(inject(AuthService)))