简体   繁体   English

如何在角度2中从子级路由重定向到父级路由

[英]how to redirect to parent route from children route in angular 2

I created child router in my router configuration and configured my child router in way that it redirects to parent router component. 我在路由器配置中创建了子路由器,并以将其重定向到父路由器组件的方式配置了子路由器。 But the configuration doesn't work as expected. 但是该配置无法按预期工作。 Instead it redirects to error page as it didn't find the configured path. 而是重定向到错误页面,因为它找不到配置的路径。

My app.route.ts file 我的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);

May I ask why you have a child route without a path? 请问您为什么有一条没有路径的孩子路线? But since it's not an actual path, you wouldn't need to redirect anywhere, since there is no change in url. 但是由于这不是实际路径,因此您无需在任何地方重定向,因为url没有变化。 I had a similar setup where I did not want to show a child initially upon navigation. 我有一个类似的设置,我不想在导航时最初显示一个孩子。 Leaving it empty worked fine for me, so try: 将其保留为空对我来说很好,所以请尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM