[英]InjectionToken for config not working with AoT
I'm writing an Angular library where I need to pass in a configuration object in the .forRoot()
of the module import. 我正在编写一个Angular库,我需要在模块导入的
.forRoot()
中传入一个配置对象 。
I'm trying to do this (the apparently correct way) with InjectionToken
, but getting an AoT compilation error. 我正在尝试使用
InjectionToken
执行此操作(显然是正确的方法),但是收到AoT编译错误。
Error when compiling with AoT: 使用AoT进行编译时出错:
error TS4050: Return type of public static method from exported class has or is using name 'InjectionToken' from external module "/path/to/node_modules/@angular/core/src/di/injection_token" but cannot be named.
错误TS4050:从导出类返回的公共静态方法类型具有或正在使用来自外部模块“/ path / to / node_modules / @ angular / core / src / di / injection_token”的名称'InjectionToken',但无法命名。
Any help is appreciated. 任何帮助表示赞赏。
module.ts: module.ts:
import { NgModule } from '@angular/core';
import { CONFIG, Config } from './config';
@NgModule({ ... })
export class SomeModule {
static forRoot(config?: Config) {
return {
ngModule: SomeModule,
providers: [
{ provide: CONFIG, useValue: config }
]
};
}
}
config.ts: config.ts:
import { InjectionToken } from '@angular/core';
export const CONFIG = new InjectionToken<Config>('config');
export interface Config {
some?: string;
values?: string;
here?: string;
}
Desired usage by another application: 其他应用程序所需的用法:
import { NgModule } from '@angular/core';
import { SomeModule } from 'some-library';
@NgModule({
imports: [
SomeModule.forRoot({
// <-- config values here
})
]
})
(Angular 5, TypeScript 2.6.2) (Angular 5,TypeScript 2.6.2)
Answer by JB Nizet in comments above... JB Nizet在上面的评论中回答......
Needed to specify the type returned by the forRoot method, ModuleWithProviders
. 需要指定forRoot方法
ModuleWithProviders
返回的类型。
import { NgModule } from '@angular/core';
import { CONFIG, Config } from './config';
@NgModule({ ... })
export class SomeModule {
static forRoot(config?: Config): ModuleWithProviders {
return {
ngModule: SomeModule,
providers: [
{ provide: CONFIG, useValue: config }
]
};
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.