ERROR in Error encountered resolving symbol values statically. Calling function calls are not supported

474 Views Asked by At

I'm trying to get my Angular CLI (1.0.0-rc.1) app to build for produciton and am getting an AOT error. I actually have 2 apps: primary app and a npm installed app for some UI widgets. The UI widgets app requires some configuration data (paths,keys,etc).

Here's my code in my primary app that assembles the config data and passes it to UI Widget module:

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:
            []
        };
    }
}

and here's the 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)
                }
            ]
        };
    }   
}

My UIWidgetModule uses a service (ClientConfigService) to hold the configuration data from client. Subsequent components use various functions in ClientConfigService to consume the config data. The kicker is ClientConfigService is static.

Here's 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;
    };
}

The build process for production (AOT) fails miserably trying to set the config value for the static variable of ClientConfigService. How do I consume the config data in my widget module? I love having a static service supply the data so I do not have to drop ClientConfigService in the constructor of every component needing it.

Thanks.

1

There are 1 best solutions below

0
On

Some how the conversation with Günter Zöchbauer was deleted. I'm not sure why or who deleted it.

Here's the code suggestion made by 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
                }
            ]
        };
    }   
}

The problem is how do I get config parameter passed into forRoot to configHelperFactory so I can populate ClientConfigService.ConfigModel?

Günter suggested making 'export const config: ConfigModel'. Making const config does not work because it's a const and has to be initialized with a value and once a const is initialized, it cannot be modified.

So, how do you populate ClientConfigService.ConfigModel with the value passed to forRoot?