I'm creating Angular Library but have some problem about environment. I generated a application(parent) and library(child). How to get environment (it defined under parent) from child component that defined in library.
I generated apps like this.
ng version
# Angular CLI: 8.0.1
# Node: 10.15.3
# OS: darwin x64
# Angular: 8.0.0
ng new app-parent
cd app-parent
ng generate library lib-child
project structure like this.
$ tree -L 2
.
├── README.md
├── angular.json
├── browserslist
├── e2e
│ ├── protractor.conf.js
│ ├── src
│ └── tsconfig.json
├── karma.conf.js
├── package.json
├── projects
│ └── lib-child # <- wanna get environment from here!!
├── src
│ ├── app
│ ├── assets
│ ├── environments
│ ├── favicon.ico
│ ├── index.html
│ ├── main.ts
│ ├── polyfills.ts
│ ├── styles.sass
│ └── test.ts
├── tsconfig.app.json
├── tsconfig.json
├── tsconfig.spec.json
├── tslint.json
└── yarn.lock
I tried import as relative path like... import { environment } from '../../../src/environment';
But this solution can NOT use another app.
Library should use some environment&apps.
I'm finding a way to like this.
import { environment } from '@angular/core';
environment.production #=> true/false
I don't think passing the environment to a side project is a good practice,
Instead try passing it through the main app by creating a static method on the project's module:
import { environment } from '../../../src/environment';
...
@NgModule({
imports: [
BrowserModule,
MyProjectModule.forRoot({
environment: environment
}),
...
]})
and in the project module:
export class MyProjectModule {
static forRoot(environment): ModuleWithProviders {
// User envirnment here or decalare an injection token for the environment
console.log(environment);
return {
ngModule: SampleModule,
providers: [{provide: 'environment', useValue: environment}]
};
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With