How to use provideAppInitializer instead of APP_INITIALIZER

Discussão em 'Angular' iniciado por Raphaël Balet, Janeiro 13, 2025.

  1. 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

    Continue reading...

Compartilhe esta Página