2017-03-03 67 views
4

我有以下的路由配置,但我得到的錯誤Invalid configuration of route 'tenant': redirectTo and children cannot be used together角重定向到默認子視圖

一次,我已經點擊/房客我想以某種方式顯示兩個租戶,然後審覈內容是什麼?這可能嗎 ?我的租客URL看起來像下面http://localhost:8080/#/tenant

{ 
      path: 'tenant', 
      redirectTo: '/tenant/(view:audit)', 
      pathMatch: 'full', 
      component: TenantComponent, 
      data: { 
       authorities: ['ROLE_USER', 'ROLE_ADMIN'], 
       pageTitle: 'tenant.home.title' 
      }, 
      children: [ 
       { 
        path: 'audit', 
        component: PacketDataComponent, 
        data: { 
         authorities: ['ROLE_USER', 'ROLE_ADMIN'], 
         pageTitle: 'tenant.home.title' 
        }, 
       } 
      ] 
     } 

回答

20

你可以使用一個空兒路由,而不是:

{ 
    path: 'tenant', 
    component: TenantComponent, 
    children: [ 
     { 
      path: '', 
      pathMatch: 'full', 
      redirectTo: 'audit' 
     }, 
     { 
      path: 'audit', 
      component: PacketDataComponent, 
      data: { 
       authorities: ['ROLE_USER', 'ROLE_ADMIN'], 
       pageTitle: 'tenant.home.title' 
      }, 
     } 
    ] 
} 
+0

你是最棒的 –

+0

@Alimohammadi謝謝,不客氣兄弟:) –

1

繼承人我的設置,這似乎工作..

import {RouterModule, Routes} from '@angular/router'; 
import {Route1Component} from './routes/route1/route1.component'; 
import {Route1DetailComponent} from './routes/route1/detail/route1-detail.component'; 
import {Route1ListComponent} from './routes/route1/list/route1-list.component'; 
import {Route2Component} from './routes/route2/route2.component'; 

const routes: Routes = [ 
    { 
    path: 'route1', 
    component: Route1Component, 
    children: [ 
     {path: ':id', component: Route1DetailComponent}, 
     {path: '', component: Route1ListComponent} 
    ] 
    }, 
    {path: 'route2', component: Route2Component}, 
    { 
    path: '', 
    pathMatch: 'prefix', 
    redirectTo: '/route1' 
    } 
]; 

export const routing = RouterModule.forRoot(routes); 

項目在.. https://github.com/danday74/angular2-coverage/blob/master/app/app.routes.ts ..如果你想捅一捅

繼承人另一個..

import {RouterModule, Routes} from '@angular/router'; 

import {ParentRouteComponent} from './routes/parent-route/parent-route.component'; 
import {ChildRoute1Component} from './routes/parent-route/child-route-1/child-route-1.component'; 
import {ChildRoute2Component} from './routes/parent-route/child-route-2/child-route-2.component'; 
import {HomeComponent} from './routes/home/home.component'; 
import {PageNotFoundComponent} from './routes/page-not-found/page-not-found.component'; 

export const routes: Routes = [ 
    { 
    path: 'parent', 
    component: ParentRouteComponent, 
    children: [ 
     { 
     path: '', 
     component: ChildRoute1Component, 
     }, 
     { 
     path: ':id', 
     component: ChildRoute2Component, 
     data: { 
      title: 'My title' 
     } 
     } 
    ] 
    }, 
    { 
    path: '', 
    component: HomeComponent 
    }, 
    { 
    path: '**', 
    component: PageNotFoundComponent 
    } 
]; 

export const routing = RouterModule.forRoot(routes); 

取自..

https://github.com/danday74/angular2-router-simple/blob/master/app/app.routes.ts

這裏**路線是後備,應列在最後。