【问题标题】:How to route to parent path while routing in Angular?在Angular中路由时如何路由到父路径?
【发布时间】:2020-10-23 08:18:52
【问题描述】:

如果我将路径定义如下:-

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

如果我直接路由到 'something/:code/first/second' 我希望它首先进入 FirstModule 即路径应该自动变为 'something/:code/first' 然后如果调用 SecondModule 路径它应该路由到那里.我如何做到这一点?

提前谢谢你!

【问题讨论】:

    标签: javascript angular routes lazy-loading


    【解决方案1】:

    你可以试试pathMatch:full 或者第二个模块必须是第一个模块的子模块

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

    【讨论】:

      【解决方案2】:

      基本上你应该将second 路由保留为FirstModule(子路由)的一部分

      const routes = [
         { path: 'second', loadChildren: './modules/second.module#SecondModule' },
         ...
      ]
      
      @NgModule({
         imports: [
            ...,
            RoutingModule.forChild([routes])
         ],
         ...
      })
      export class FirstModule {}
      

      然后SecondModule会有SecondModule相关路由,默认''路径会加载需要的组件。

      const routes = [
         { path: '', component: SecondComponent },
         ...
      ]
      
      @NgModule({
         imports: [
            ...,
            RoutingModule.forChild([routes])
         ],
         ...
      })
      export class SecondModule {}
      

      【讨论】:

        猜你喜欢
        • 2019-03-28
        • 2017-11-10
        • 2016-11-25
        • 2018-12-20
        • 2017-09-18
        • 1970-01-01
        • 2016-12-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多