Raphaël Balet
Raphaël Balet

Reputation: 8441

How to use provideAppInitializer instead of APP_INITIALIZER

I have a library that should run when the app boots.

I see that Angular 19 now use provideAppInitializer. I did use the automatic angular ng update migration cli and it changed the code.

// From 
{
  provide: APP_INITIALIZER,
  useFactory: (service: NgxBackButtonService) => () => service,
  deps: [NgxBackButtonService],
  multi: true,
},
// To
provideAppInitializer(() => {
  const initializerFn = ((service: NgxBackButtonService) => () => service)(inject(NgxBackButtonService))
  return initializerFn()
}

But this doesn't work and throw me the following error

Argument of type '() => NgxBackButtonService' is not assignable to parameter of type '() => void | Observable<unknown> | Promise<unknown>'.
  Type 'NgxBackButtonService' is not assignable to type 'void | Observable<unknown> | Promise<unknown>'.ts(2345)

Does someone knows how I should adapt it ?

Here the Stackblitz code

Upvotes: 2

Views: 612

Answers (1)

Matthieu Riegler
Matthieu Riegler

Reputation: 54485

If you just need your service to be instantiated, just inject it.

provideAppInitializer(() => {
   inject(NgxBackButtonService)
}

Upvotes: 3

Related Questions