0

I have a CoreModule (which provides common functionalities like providing customised wrapper service on top of HttpClient etc. - as npm package), and AppModule (Angular application which installs CoreModule as npm package).

CoreModule has many things regarding configuration (i.e. login URL, base URL for REST endpoints, etc.). So whenever I need to fetch data from the server, I just give endpoint URL to the services exposed by CoreModule and then it takes care of rest of the things like creating complete request URL, making ajax call, providing response, etc.

Now, is there any way so that I can update (override) the base URL (of CoreModule) from my AppModule?

In Angular 1.x, we used to have value recipe. So, we could do like

angular.module('myApp').config(['$provide', function($provider){
   $provider.decorator('configVarsInCoreModule', ['$delegate', function($delegate){
       // give configParamFromClientApp
       angular.extend($delegate, configParamFromClientApp);
   }]);
}]);

Is there any similar (or different) way available in Angular 4+ to achieve similar kind of behaviour?

The thing is, once I update/override the baseURL (or any other config variable) from client app, it should be updated for the CoreModule as well. So if any other service/component tries to access the variable (even from CoreModule), it should read the updated value.

Paritosh
  • 11,144
  • 5
  • 56
  • 74
  • Well, did you try `useClass` , `useExisting` ? i am also not sure but i think it might do the trick . – Shashank Vivek Apr 03 '18 at 08:23
  • Yes it is really nice idea. You can provide config class to DI and then use values and functionality (e.g. setBaseUrl) of that class where it is needed. – Vayrex Apr 03 '18 at 08:28
  • will that do the trick even for `CoreModule` along with `AppModule`? – Paritosh Apr 03 '18 at 08:28
  • Core module will provide this class to DI. Then you will inject it where it is needed, so instance must be the same for whole app. – Vayrex Apr 03 '18 at 08:34
  • I am not sure, have to check, coz I think only `AppModule` will get the updated value (the value to be updated is provided by the client app only), `CoreModule` won't as I won't be providing that class at `CoreModule` level. – Paritosh Apr 03 '18 at 08:40

0 Answers0