2017-03-20 18 views
0

我正在用angular-cli構建一個項目,在升級到1.0.0-rc.2版本之後,我目前正在向ng build發佈該應用的問題。當我嘗試這樣做,我收到以下錯誤:使用angular-cli @建設時提供商的問題最新

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 {} 

您知道什麼是解決此問題的最佳方法嗎?提前感謝您的回覆!

回答

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 {} 

它應該工作。

希望這會有所幫助!

+0

謝謝你,但錯誤仍然存​​在。顯然我必須將函數存儲在服務或變量中,以這種方式傳遞值 –

+0

您可以嘗試在同一個文件中用@NgModule裝飾器中的函數名稱聲明您的useFactory函數,並使用函數名稱在useFactory中引用它們 –

+0

我有trie使用全局變量或導出常量,但它再次拋出錯誤。你會如何申報? –