I'm writing an Angular library named where I need to pass in a configuration object in the.forRoot() of the module import.的 Angular 库,我需要在模块导入的 .forRoot() 中传入一个配置对象。
I'm trying to do this with InjectionToken, but when I run my application(installed build with ) with ng build--prod
, I got an error just like this安装的构建)时,我收到了这样的错误
ERROR in Error during template compile of 'TestModule' Function calls are not supported in decorators but 'libModule' was called.
import { NgModule } from '@angular/core';
import { CONFIG, Config } from './config';
import { LibService } from './libService';
@NgModule({ ... })
export class LibModule {
static forRoot(config?: any): ModuleWithProviders {
return {
ngModule: LibModule,
providers: [
LibService,
{ provide: CONFIG, useValue: config }
]
};
}
}
import { InjectionToken } from '@angular/core';
export const CONFIG = new InjectionToken<Config>('config');
export interface Config {
key?: string;
}
import { Observable } from "rxjs/Observable";
import { Injectable, Inject } from "@angular/core";
import { Config, LIB_CONFIG } from "./config";
@Injectable()
export class LibService {
private key: string;
constructor(
@Inject(LIB_CONFIG) config: any
) {
this.key = config.KEY;
console.log('ewrwerwerwrewrewr'+this.key);
}
}
import { NgModule } from '@angular/core';
import { LibModule } from 'some-library';
@NgModule({
imports: [
libModule.forRoot({
// <-- config values here
})
]
})
For my case, I remove the debug statement before return
of forRoot
function.
Reference: https://github.com/angular/angular-cli/issues/9358#issuecomment-435288467
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.