1

我正在尝试让我的 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。

谢谢。

4

1 回答 1

0

一些与 Günter Zöchbauer 的对话是如何被删除的。我不确定为什么或谁删除了它。

这是 Günter 提出的代码建议:

export const config: ConfigModel = new ConfigModel();
export function configHelperFactory() 
{
    console.log('CONFIG:', config);
    ClientConfigService.ConfigModel = config;
}

@NgModule({
    imports:
    [
        CommonModule
    ],
    declarations:
    [
        //COMPONENTS
    ],
    exports:
    [
        //COMPONENTS
    ],
    entryComponents:
    [
        //COMPONENTS
    ],
    providers:
    [
    ]
})
export class UiWidgetModule
{
    static forRoot(config: ConfigModel): ModuleWithProviders
    {   
        return {
            ngModule: UiWidgetModule,
            providers:
            [
                ClientConfigService,
                {
                    provide: ClientConfigService,
                    useFactory: configHelperFactory
                }
            ]
        };
    }   
}

问题是如何将配置参数传递给 forRoot 到 configHelperFactory,以便我可以填充 ClientConfigService.ConfigModel?

Günter 建议制作“export const config: ConfigModel”。使 const config 不起作用,因为它是一个 const 并且必须用一个值初始化,并且一旦 const 被初始化,就不能修改它。

那么,如何使用传递给 forRoot 的值填充 ClientConfigService.ConfigModel?

于 2017-03-08T17:14:26.027 回答