Angular2-路由重定向的办法

  使用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' }
]