0

I am creating an angular 4 node package for a logging service that we have created. Is it possible to use the angular cli environment variables to set the default path for the service? If so, how should the path be defined for the environment file?

I am following these instructions for creating the node package: https://medium.com/@cyrilletuzi/how-to-build-and-publish-an-angular-module-7ad19c0b4464

I followed these examples for setting up environment variables: http://tattoocoder.com/angular-cli-using-the-environment-option/

https://medium.com/beautiful-angular/angular-2-and-environment-variables-59c57ba643be

1 Answer 1

2

You can do that using the forRoot methodology explained in the Angular docs here.

Inside of your external Angular library modify the main exported module to have this structure:

export class MyLibraryModule {
  static forRoot(config: configData): ModuleWithProviders {
    return {
      ngModule: MyLibraryModule,
      providers: [
        {provide: AppConfigModel, useValue: config }
      ]
    };
  }

  constructor(@Optional() config: AppConfigModel) {
    if (config) { /* Save config data */ }
  }

 }

 export class AppConfigModel {
    defaultPath: string;
 }

When you import this module into your application, use the forRoot static method and pass in the config data.

imports: [
    MyLibraryModule.forRoot({ defaultPath: environment.defaultPath }),
 ]

On a side note, I use this library for creating my Angular npm modules. It simplifies the process quite a bit.

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

Comments

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.