【问题标题】:ERROR in Error encountered resolving symbol values statically. Calling function calls are not supported错误中的错误遇到静态解析符号值。不支持调用函数调用
【发布时间】:2017-07-28 17:00:32
【问题描述】:

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

谢谢。

【问题讨论】:

  • cmets怎么被删除了?
  • @Günter Zöchbauer - 我们的谈话发生了什么?

标签: angular angular-cli angular2-aot angular-module angular2-bootstrapping


【解决方案1】:

与 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-07-06
    • 1970-01-01
    • 2017-06-07
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    相关资源
    最近更新 更多