2016-07-24 114 views
2

以下路由配置有什麼問題?即使有路線app/jungle,我也總是導航到**角路由器3通配符匹配

import {bootstrap} from '@angular/platform-browser-dynamic'; 


import { RouterConfig, provideRouter } from '@angular/[email protected]' 

import {App} from './app'; 
import { HomeComponent } from './home.component'; 
import { JungleComponent } from './jungle.component'; 
import { NotFoundComponent } from './not-found.component'; 



const APP_ROUTES: RouterConfig = [ 
    { 
    path: '', pathMatch: '', redirectTo: 'app/home' 
    }, 
    { 
    path: 'app/', pathMatch: '', redirectTo: 'app/home' 
    }, 
    { 
    path: 'app/home', component: HomeComponent 
    }, 
    { 
    path: 'app/jungle', component: JungleComponent 
    }, 
    { 
    path: '**', component: NotFoundComponent 
    } 
] 

bootstrap(App, [provideRouter(APP_ROUTES)]) 
    .catch(err => console.error(err)); 

Here是一個笨蛋。 我使用@角/路由器@ 3.0.0-beta.2

回答

3

''pathMatch一個無效值。

pathMatch支持fullprefixprefix是默認值。

它設置爲'full'前兩個途徑:

{ 
    path: '', pathMatch: 'full', redirectTo: 'app/home' 
}, 

{ 
    path: 'app/', pathMatch: 'full', redirectTo: 'app/home' 
}, 

{ 
path: 'app/home', component: HomeComponent 
}, 

{ 
    path: 'app/jungle', component: JungleComponent 
}, 

{ 
    path: '**', component: NotFoundComponent} 
] 

Plunker example

更新(根據下面的評論)

我完全不知道爲什麼尾隨/使其工作,但我會使用無組件的父路線,而不是像

const APP_ROUTES: RouterConfig = [ 
{ path: '', pathMatch: 'full', redirectTo: 'app/home' }, 
{ path: 'app', children: [ 
    { path: '', pathMatch: 'full', redirectTo: 'home' }, 
    { path: 'home', component: HomeComponent }, 
    { path: 'jungle', component: JungleComponent }, 
]}, 
{ path: '**', component: NotFoundComponent }] 

Plunker example

+0

哇,這實際上解決了這個問題,但如果我改變了'{ 道: '應用程序/',pathMatch: '全',redirectTo: '應用程序/家' }'來'{ 路徑:'app',pathMatch:'full',redirectTo:'app/home' }(**注意路徑屬性**末尾的反斜槓)給出了和以前相同的問題。任何線索 – Lekhnath

+0

我沒有看到斜線。我會再檢查一次。 –

+0

我更新了我的答案。 –