简体   繁体   English

在Angular中路由时如何路由到父路径?

[英]How to route to parent path while routing in Angular?

If I define my paths as follows :-如果我定义我的路径如下:-

   {
    path: 'something/:code/first',
    loadChildren: './modules/first.module#FirstModule'
   },
  {
    path: 'something/:code/first/second',
    loadChildren: './modules/second.module#SecondModule'
  },

If I directly route to 'something/:code/first/second' I expect it to first enter the FirstModule ie path should automatically become 'something/:code/first' and then if SecondModule path is invoked it should route there.如果我直接路由到 'something/:code/first/second' 我希望它首先进入 FirstModule 即路径应该自动变成 'something/:code/first' 然后如果调用 SecondModule 路径它应该路由到那里。 How do I achieve this?我如何实现这一目标?

Thank you in advance!先感谢您!

You can try with pathMatch:full or the second module has to be child of first module您可以尝试使用pathMatch:full或第二个模块必须是第一个模块的孩子

{
    path: 'something/:code/first/second',
    pathMatch:'full'
    loadChildren: './modules/second.module#SecondModule'
  }

Basically you should keep second route as a part of FirstModule (child route)基本上你应该保留second条路线作为FirstModule (子路线)的一部分

const routes = [
   { path: 'second', loadChildren: './modules/second.module#SecondModule' },
   ...
]

@NgModule({
   imports: [
      ...,
      RoutingModule.forChild([routes])
   ],
   ...
})
export class FirstModule {}

And then SecondModule will have the SecondModule related routes, and default '' path will load desired component.然后SecondModule将拥有与 SecondModule 相关的路由,并且默认的''路径将加载所需的组件。

const routes = [
   { path: '', component: SecondComponent },
   ...
]

@NgModule({
   imports: [
      ...,
      RoutingModule.forChild([routes])
   ],
   ...
})
export class SecondModule {}

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

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