我正在尝试让我的 Angular CLI (1.0.0-rc.1) 应用程序为生产而构建,但我遇到了 AOT 错误。我实际上有 2 个应用程序:主要应用程序和一些 UI 小部件的 npm 安装应用程序。UI 小部件应用程序需要一些配置数据(路径、键等)。
这是我的主要应用程序中的代码,它组装配置数据并将其传递给 UI Widget 模块:
export const config: ConfigData =
{
UrlRoot: 'XXXX',
Key: 'ZZZZ'
}
@NgModule({
imports:
[
UiWidgetModule.forRoot(config)
],
declarations:
[],
exports:
[]
})
export class CoreModule
{
static forRoot(): ModuleWithProviders
{
return {
ngModule: CoreModule,
providers:
[]
};
}
}
这是 UIWidgetModule:
export function configHelperFactory(config: ConfigData)
{
ClientConfigService.ConfigModel = config;
return ClientConfigService;
}
@NgModule({
imports:
[
CommonModule
],
declarations:
[
//COMPONENTS
],
exports:
[
//COMPONENTS
],
entryComponents:
[
//COMPONENTS
],
providers:
[
]
})
export class UiWidgetModule
{
static forRoot(config: ConfigData): ModuleWithProviders
{
return {
ngModule: UiWidgetModule,
providers:
[
ClientConfigService,
{
provide: ClientConfigService,
useFactory: configHelperFactory(config)
}
]
};
}
}
我的 UIWidgetModule 使用服务 (ClientConfigService) 来保存来自客户端的配置数据。后续组件使用 ClientConfigService 中的各种函数来消费配置数据。关键是 ClientConfigService 是静态的。
这是 ClientConfigService :
import { ConfigData } from "../models/index";
export class ClientConfigService
{
static ConfigModel: ConfigData ;
static BuildMediaUrl(nodeId: string) : string
{
return ClientConfigService.ConfigModel.UrlRoot+ '/' + nodeId;
};
static GetKey(): string
{
return ClientConfigService.ConfigModel.Key;
};
}
生产(AOT)的构建过程在尝试为 ClientConfigService 的静态变量设置配置值时惨遭失败。如何使用小部件模块中的配置数据?我喜欢有一个静态服务来提供数据,所以我不必在每个需要它的组件的构造函数中删除 ClientConfigService。
谢谢。