0

My problem is as follows: I use the package api-client-generator (https://www.npmjs.com/package/api-client-generator) which uses two InjectionTokens to build API requests. These tokens include the API domain and HTTP options.

In my application, I only receive information about the API domain and the HTTP options (clientId etc.) at a later point in time. The data is requested asynchronously and then written to a store via NGRX.

Now I would like to somehow provide the asynchronously received data via the InjectionTokens. However, these are built before the application is bootstrapped. Now, since the latest Angular versions (I use NG19), there is provideAppInitializer as a way to initialize data before bootstrapping.

What is noticeable, however, is that I previously used the APP_INITIALIZER token in Angular 17 and the factory call to this token was executed BEFORE all other InjectionTokens were built. This made it possible to reference data in the InjectionTokens within the AppModule that was previously retrieved within APP_INITIALIZER, e.g. via API request, and stored in a central location (here in the NGRX store).

Since Angular 19, the hierarchy has (apparently) changed. The InjectionTokens are still built before provideAppInitializer and only then is the function referenced in provideAppInitializer executed.

As a result, some data within the InjectionTokens is undefinded because it has not yet been retrieved via the API.

Is it always the case that provideAppInitializer is only called after the InjectionTokens have been created? Is there an intelligent alternative how I can provide the two tokens for the api-client-generator only after I have received the data by request via an API interface?

Calling provideAppInitializer in main.ts to retrieve necessary data before creation of InjectionTokens. Unfortunately provideAppInitializer gets called after creation of InjectionTokens (in my case, Angular 19).

2
  • provideAppInitializer is just a wrapper around APP_INITIALIZER there should be absolutely no difference. If you can provide a repro that exhibits such issue, it would be interested ! Commented Feb 27 at 11:23
  • I was actually able to solve the problem. FYI: I injected a service within a store that could not exist at that time. This did not lead to an error during compiling, but to the rotated sequence of 1. provideAppInitializer > 2. building the InjectionTokens to 1. building the InjectionTokens > 2. provideAppInitializer. I therefore recommend everyone with a similar problem to check whether a service is injected in the constructor via DI in one of the stores used that is causing the problem. Thanks for your explanation anyway. @MatthieuRiegler Commented Feb 27 at 13:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.