使用Angular2開發,常會遇到路由重定向的應用場景。
路由重定向的配置辦法是這樣的:
{path:'要定向的路徑', redirectTo:'要定向到的目標路由'}
比如某組件有個路由插件,並且一進入該組件就想要路由插件加載出內容,我們的路由配置可能這么寫:
routes: Routes = [
{
path:'',
component:'ParentComponent',
children:[
{ path:'', redirectTo: 'page1' },//定向
{ path:'page1', component:'PageComponent1' },
{ path:'page2', component:'PageComponent2' }
]
}
]
如果是兄弟路由之間的定向,則類似這樣:
routes: Routes = [
{ path:'', redirectTo: 'page1' },
{ path:'page1', component:'PageComponent1' },
{ path:'page2', component:'PageComponent2' }
]
