1

The scenario is we have Angular 6 Library (we built) included as an NPM package in the main app. The main app uses Angular CLI environments with specific configs per environment (as usual). Currently the library is configured like MyModule.forRoot( environment.whateverConfig ) in app.module.ts.

The issue is some of the settings in environment.whateverConfig need to be provided by a call to the server for which we are using APP_INITIALIZER. But the problem is forRoot() is executed before the promise in the APP_INITIALIZER provider finishes, so the library does not get the correct config.

Seems APP_INITIALIZER is good for doing things before the app bootstraps but not before libraries/modules are loaded.

Looking for a solution or workaround for this.

Thanks.

1 Answer 1

1

My previous answer to a similar question should help you. Take a look at the asynchronous initialization method there.

The gist of it is that instead of getting the configuration, you get an Observable of the configuration, and then anywhere that you want to use that, you flatMap (a.k.a. mergeMap) over the configuration observable. This essentially lets consumers of the configuration wait for it to be loaded.

Sign up to request clarification or add additional context in comments.

2 Comments

GreyBeardedGeek thanks for the reply. I read up on your solution and unless I do not understand the Config Service needs to be injected into the services in the Library. The library would now have a dependency on a service of the app making it a less-reusable library. I want the library to be an independent NPM module that can be re-used by other Angular apps in the organization.
Yes, the ConfigService does need to be injected into the app's services. I put the ConfigSevice into the same NPM module as the initialization code, so that the apps are still just dependent on the one NPM module, and the NPM module is not dependent on anything in the app.

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.