2017-10-06 107 views
0

這是我的路由器供應商的聲明:Angular4 - 路由器模塊提供商AOT

@NgModule({ 
    imports: [RouterModule.forRoot(RoutesFactory.buildRoutes())], 
    exports: [RouterModule] 
}) 
export class RouterModuleProvider { }; 

在AOT編譯,我得到這個錯誤:

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, resolving symbol RouterModuleProvider

這裏是RoutesFactory.buildRoutes

的內容
static buildRoutes() { 
    let returnRoutes: Routes = [ 
     { path: '', redirectTo: '/login', pathMatch: 'full' }, 
     { path: 'login', component: ViewsModule.LoginViewComponent } 
    ]; 

    let appendRoutes = (items: MenuItem[], routes: Routes) => { 
     for (let item of items) { 
      let route: Route = { 
       path: item.Path, component: item.Component, children: [] 
      }; 

      if (item.Children != null) 
       appendRoutes(item.Children, route.children) 

      routes.push(route); 
     } 
    }; 

    appendRoutes(RoutesFactory.getMenus(), returnRoutes); 

    return returnRoutes; 

} 

RoutesFactory.getMenus()返回一個靜態的內部對象數組。 (沒有從後端加載)

有什麼辦法可以解決這個問題嗎?

+0

你在RoutesFactory.buildRoutes()中有什麼? –

+0

我編輯了自己的問題,以便提供有關此問題的更多詳細信息 – gio

+0

這看起來像是一種非常奇怪的做aot路由的方式。任何理由你這樣做? – mast3rd3mon

回答

1

不幸的是,如錯誤所示,作爲元數據一部分的所有內容(在@NgModule(...)聲明中)必須是靜態可解析的。這意味着您的buildRoutes函數中不能有任何邏輯,只有靜態聲明。

但是,似乎有一個專門用於動態定義路由的替代方案。請看這個答案:Dynamic routing based on external data

+0

我會看看你的建議。謝謝你的提示。 – gio