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 ?
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!
I found an alternative solution to this problem in a GitHub issue. The solution in the GitHub thread has a bug (a typo), so I'm including the fixed solution here:
To begin, add a provider to your top-level AppModule that contains your environment file.
import {environment} from '../environments/environment'
@NgModule({
providers: [
{provide: 'environment', useValue: environment}
]
// object properties omitted for brevity...
})
class AppModule {}
Finally, make use of an Inject decorator to include your environment file in any other part of your application you wish (library or otherwise):
@Component({
// object properties omitted for brevity
})
class MyComponent {
private environment
constructor(
@Inject('environment')
environment
) {
this.environment = environment
}
}
If You are still looking for the answer.
In the current version i.e. Angular > 6, you don't have to do anything.
The angular-cli commands "ng build --prod (for Production)& ng build (for Development)" will take care of it for you.
Example: If you are running the project in development environment, all the variables are captured from src/environments/environment.ts.
In your component library project just import "import { environment } from 'environments/environment';" (please make sure about the path) will takecare of the environment depending upon the angular-cli build command.