2017-06-12 38 views
0

我想創建RouteConfig動態這樣角2 RouteConfig

for (let route of routes) { 

    { path: route.name , component: route.component } 

} 

不喜歡這樣的:

{ path: '', redirectTo: 'Home', pathMatch: 'full' }, 
{ path: 'Home', component: HomeComponent}, 
{ path: 'Info' , component: InfoComponent }, 
{ path: 'About' , component: AboutComponent }, 

任何的例子嗎?

回答

0

你已經回答了你的問題。角度中的路由只是對象的數組。所以你可以創建變量和推送對象(它是路由接口)給變量。並將該變量傳遞給RouteModule.forChild()forRoot()方法。

const coreRoutes:Routes = (function(){ 
    let result_routes = []; 
    for (let route of routes) { 
    result_routes.push({ path: route.name , component: route.component }); 
    } 
    return result_routes; 
})(); 


@NgModule({ 
    imports: [ 
    RouterModule.forChild(coreRoutes) 
    ], 
    exports: [ 
    RouterModule 
    ], 
    providers:[] 
}) 

在上面我用自調用函數make變量不visble到global.It的不必要的例子,但它是很好的避免名稱衝突的事情。