2017-05-24 87 views
1

我有一堆模塊,它們在運行時根據JSON文件的內容設置其路由(這意味着我在這裏使用函數,除非我想打破我以前的邏輯)。我有一個數據解析成一個Routes對象的函數,然後返回它:Angular 2:不支持函數調用

export function getRoutes(): Routes { 
    let routes: Routes = [ 
     //default component 
     { 
      path: '', 
      component: AComponent 
     } 
    ]; 

    for(let module of APP_CONFIG.modules){ 
     routes.push({ 
      path: module.path, 
      loadChildren: module.modulePath 
     }) 
    } 
    return routes; 
} 

然後我只需撥打我module功能:

@NgModule({ 
    declarations: [ 
    //[...] 
    ], 
    imports: [ 
    //[...] 
    RouterModule.forRoot(getRoutes()) 
    ], 
    providers: [ 
    //[...] 
    ], 
    bootstrap: [ AppComponent ] 
}) 
export class AppModule { } 

然而,我的項目(這是產生由角-CLI)不會建:

ERROR in Error encountered resolving symbol values statically. Calling function 'getRoutes', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function 

我隨後在錯誤消息中的指令,並且它內置一次。然後它繼續用同樣的信息一遍又一遍地重複故事。

所以我想我的問題是:我該如何解決這個愚蠢的問題?

+0

你找到一個解決方案對此? – adamdport

+0

沒有。我仍然在搜尋...... :( –

回答

0

AoT不支持直接使用路由功能。避免使用它或從其他模塊中導出它。

不要:

function random() { 
    return [{ 
    path: "", 
    component: AppViewComponent 
    }]; 
} 
const SAMPLE_APP_ROUTES: Routes = random(); 

務必:

const SAMPLE_APP_ROUTES: Routes = [{ 
    path: "", 
    component: AppViewComponent 
}]; 

import { random } from "./random.routes.ts"; 
const SAMPLE_APP_ROUTES: Routes = random(); 

來源:https://github.com/rangle/angular-2-aot-sandbox#func-in-routes-top

+0

我試過這個;這是我說'我按照錯誤信息中的指示'的意思,它建立了**一次**。 –

相關問題