如何在 angular 2 中从子路由重定向到父路由

how to redirect to parent route from children route in angular 2

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

我的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);

请问为什么你的child路由没有路径?但由于它不是实际路径,您不需要重定向到任何地方,因为 url 没有变化。我有一个类似的设置,我不想在导航时最初显示 child 。把它留空对我来说很好,所以试试:

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