A while ago we started creating internal NPM packages with angular components in them so we can reuse our common angular components between our different sites. With some new work we we are doing we're trying to support AOT compilation as well as having the components in the NPM package compatible with both angular 2.x and 4.x. We have a few configurations that need to be passed into the npm package/library (api keys/urls, environment, etc) so I would like to have the implementing site provide a config object that can be injected into the components that need those values. I am having issues getting the providers to work with AOT. So far, the only way I've been able to get this to work is to use an explicit string token, but this is far from ideal in my eyes.
Working:
In the consuming AOT site:
import { environment } from './../environments/environment';
@NgModule({
declarations: [
...
],
imports: [
...
],
providers: [
{
provide: 'ConfigToken',
useValue: environment
}
],
bootstrap: [AppComponent]
})
export class AppModule { }
In the library components:
constructor(@Inject('ConfigToken') private config: Configuration) { }
Interface exported from library:
export interface Configuration {
someValue: string;
someOtherValue: string;
}
I have tried:
exporting an
OpaqueToken
from the libraryexporting a class to use as a token
With both of those strategies, I end up with an error
ERROR in Can't resolve all parameters for SomeComponent in C:/Development/path/to/bundle