Adding Library configs using `forRoot()` and `InjectionToken`
See original GitHub issueI’m having trouble adding library configs using forRoot()
and InjectionToken
.
When passing in environment variables via forRoot(config)
, I can access the variables in the forRoot
method, but they are not injecting into services.
I’ve added some comments in my sample code to show where things are breaking.
I originally generated my project using generator-angular2-library@10.1.0
app.module.ts (App that uses Library)
@NgModule({
declarations: [AppComponent],
imports: [
CoreModule.forRoot(environment) // {key: 'foobar'}
],
providers: [...],
bootstrap: [AppComponent],
})
export class AppModule { }
index.ts (Library)
import { NgModule, ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Config, LIB_CONFIG } from './config';
import { FooService } from './foo.service';
@NgModule({
imports: [CommonModule],
providers: [FooService],
})
export class CoreModule {
static forRoot(config: Config): ModuleWithProviders {
console.log(config); // prints: `{key: 'foobar'}`
return {
ngModule: CoreModule,
providers: [
{provide: LIB_CONFIG, useValue: config} // If I hard code `useValue: {key: 'FooBar'}`, instead of using `config` it works... weird.
],
};
}
}
config.ts (Library)
import { InjectionToken } from '@angular/core';
export interface Config {
key?: string;
}
export const LIB_CONFIG = new InjectionToken<Config>('LIB_CONFIG');
foo.service.ts (Library)
import { Inject, Injectable } from '@angular/core';
import { Config, LIB_CONFIG } from './config';
@Injectable()
export class FooService {
private key: string;
constructor(
@Inject(WEB_CONFIG) private config: Config,
) {
console.log(config); // prints: `undefined`
this.key = config.key; // can't read property 'key' of undefined
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:16
- Comments:18
Top Results From Across the Web
Adding Library configs using `forRoot()` and `InjectionToken ...
I'm writing an Angular library named libModule where I need to pass in a configuration object in the .forRoot() of the module import....
Read more >ForRoot() & InjectionToken() in action — Angular
Problem: We want to create a shared module which will contain a config to set boolean values (as flags) to enable or disable...
Read more >Providing Module Configuration Using forRoot() And Ahead ...
Yesterday, I was trying to design a feature module in Angular 7.2.0 that used the .forRoot() pattern to provide optional configuration data ...
Read more >Creating configurable libraries with angular cli
... into a library the using the Angular cli environment settings is no ... add the forRoot static function to the library module...
Read more >InjectionToken - Angular
As you can see in the Tree-shakable InjectionToken example below. Additionally, if a factory is specified you can also specify the providedIn option,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey, I’ve got exactly same issue but with one difference: this occurs only with -prod enabled
Hey @brentchow, could you share that “different way” of doing this? I’m facing similar problems. Thanks.