Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing environment variables to angular library

I've created company internal library using angualr2-library yeoman generator.

Some of the angular services are using environment variables in our current applications (api endpoints are changed on each env). I was wondering what is the best way to pass the current environment object to the angular2 library services?

like image 419
Dima Grossman Avatar asked Apr 20 '17 21:04

Dima Grossman


People also ask

Can Angular access environment variables?

Introduction. If you're building an app that uses an API, you'll want to use your API key for testing environments during development and your API key for live environments during production. In Angular, you can create environment variables with the environment.

Can we use process ENV in Angular?

process. env is available to Node applications, which an Angular application is not. You have several options: Make a task of some sort in your build pipeline to update the environment file with the correct value if it truly needs to be dynamic.

What is the use of environment TS in Angular?

A project's src/environments/ folder contains the base configuration file, environment. ts , which provides a default environment. You can add override defaults for additional environments, such as production and staging, in target-specific configuration files.


1 Answers

In case you still searching for a solution, here's how I accomplished something simliar to what you were asking for (using Angular 4.2.4).

In your AppModule (or the place where you want to import your library), call the forRoot() method on your LibraryModule. With the help of this function, you can pass any config values to you library, e.g. your app's environment.

import {environment} from "../environments/environment"; ...  @NgModule({     declarations: [         AppComponent     ],     imports: [         BrowserModule,         ...         LibraryModule.forRoot(environment)     ],     bootstrap: [AppComponent] }) export class AppModule { } 

You LibraryModule of course needs to offer the forRoot() method. In the providers array you then can provide services, values and more. In this case, 'env' acts as the token holding the given environment object for simplicity. You can also use an InjectionToken instead.

@NgModule({     ... }) export class LibraryModule {      public static forRoot(environment: any): ModuleWithProviders {          return {             ngModule: LibraryModule,             providers: [                 ImageService,                 {                     provide: 'env', // you can also use InjectionToken                     useValue: environment                 }             ]         };     } } 

Since the token env is now provided by your LibraryModule, you can inject it in all of its child services or components.

@Injectable() export class ImageService {      constructor(private http: Http, @Inject('env') private env) {     }      load(): Observable<any> {         // assume apiUrl exists in you app's environment:         return this.http.get(`${this.env.apiUrl}/images`)             .map(res => res.json());     }  } 

I hope that helps!

like image 113
pschild Avatar answered Sep 16 '22 18:09

pschild