如何从角度2的子路由重定向到父路由

时间:2017-03-28 14:54:35

标签: angularjs angular routing angular2-routing angular2-template

我在路由器配置中创建了子路由器,并以重定向到父路由器组件的方式配置了我的子路由器。但配置无法按预期工作。相反,它会重定向到错误页面,因为它找不到配置的路径。

我的app.route.ts文件

import { ModuleWithProviders }  from '@angular/core';
import { CanActivate, Routes, RouterModule } from '@angular/router';
// Component
import { HomeRouterComponent } from '../../modules/home/homerouter.component';
import { ChannelComponent } from '../../modules/channels/channel.component';

import { ErrorComponent } from '../../modules/errorPage/error.component';



const appRoutes: Routes = [
  {
    path: 'home',
    component: HomeRouterComponent,
    canActivate: [LoginCheck]
  },
  {
    path: '',
    redirectTo: 'home',
    pathMatch: 'full',
  },
  {
    path: 'channel',
    component: ChannelComponent,
    children: [
      {
        path: '',
        redirectTo: 'channel',
        pathMatch: 'full',
      }
    ]
  },
   {
    path: '**',
    component: ErrorComponent
  }
];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

1 个答案:

答案 0 :(得分:0)

请问为什么你有一条没有路径的儿童路线?但由于它不是实际路径,因此您不需要在任何地方重定向,因为URL没有变化。我有一个类似的设置,我不想在导航时向孩子展示。将它留空对我来说很好,所以试试:

  {
    path: 'channel',
    component: ChannelComponent,
    children: [
      {
        path: '',
      }
    ]
  }