使用angular-cli @ latest建立提供程序时出现问题

时间:2017-03-20 15:04:14

标签: angular angular-cli

我正在使用angular-cli构建项目,在升级到版本1.0.0-rc.2之后,我目前遇到了ng build该应用的问题。当我尝试这样做时,我收到以下错误:

ERROR in Error encountered resolving symbol values statically. Function calls are not supported. Consider replacing the function or lambda with a reference to an exported function (position 46:16 in the original .ts file), resolving symbol AppModule in /Users/Rkok/Documents/Projects/capitola-vr-frontend/src/app/app.module.ts

错误位于app.module.ts,并且已连接到提供商useFactory内的APP_INITIALIZER属性。这是完整的代码:

@NgModule({
    imports: [
        // Modules list
    ],
    declarations: [
        // Declarations list
    ],
    providers: [
        { provide: 'Window', useValue: Window },
        PostsService,
        UserService,
        {
            provide: APP_INITIALIZER, 
            useFactory: (users: UserService) => () => users.onResize(), // The error is in this line
            deps: [UserService],
            multi: true
        },
        {
            provide: APP_INITIALIZER,
            useFactory: (posts: PostsService) => () => posts.loadData(), // And in this other one
            deps: [PostsService],
            multi: true
        }
    ],
    bootstrap: [AppComponent]
})
export class AppModule {}

您知道解决此问题的最佳方法是什么吗?提前感谢您的回复!

2 个答案:

答案 0 :(得分:1)

即使我们遇到过这个问题,但如果我们声明使用普通的“函数”语法,它就可以使用ES6“=>”而不是语法。

更新后的代码如下所示:

function users(users: UserService) {
   users.onResize()
}

function posts(posts: PostsService) {
  posts.loadData()
}

@NgModule({
imports: [
    // Modules list
],
declarations: [
    // Declarations list
],
providers: [
    { provide: 'Window', useValue: Window },
    PostsService,
    UserService,
    {
        provide: APP_INITIALIZER, 
        useFactory: users,
        deps: [UserService],
        multi: true
    },
    {
        provide: APP_INITIALIZER,
        useFactory: posts,
        deps: [PostsService],
        multi: true
    }
],
bootstrap: [AppComponent]
})
export class AppModule {}

它应该有用。

希望这有帮助!

答案 1 :(得分:1)

我的问题通过这样从app模块中导出功能来解决:

export function users(users: UserService) {
   users.onResize()
}