如何使用每个子模块单独的routing.module.ts文件创建嵌套路由结构

时间:2018-01-12 15:56:18

标签: angular typescript angular4-router

我的应用程序具有如下的路由结构

const routes: Routes = [
  {
    path: 'users/:userId/product/:productId',
    component: AppComponent,
    data: { animation: 'app' },
    children: [
      {
        path: 'grouping',
        component: GroupingComponent,
        children: [
        {
            path: 'overview',
            component: OverviewComponent,
            data: { animation: 'overview' }
        },
        {
            path: 'configuration',
            component: GroupConfigurationComponent,
            data: { animation: 'configuration' }
        }
      ]
     }
    ]
  },
  {
    path: 'grouping',
    redirectTo: 'users/:userId/product/:productId/grouping/overview',
    pathMatch: 'full'
  }
];

我有一个嵌套在主路由器插座内的路由器插座。它显示分组组件的子路由。

如何使用每个子模块单独的routing.module.ts文件创建此嵌套路由结构? (我有一个GroupingModule

1 个答案:

答案 0 :(得分:0)

我会发布我的例子:

应用路由

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AppComponent } from './app.component';

const routes: Routes = [
  {
    path: ':type/firstPage.html',
    pathMatch: 'full',
    redirectTo: ':type/firstPage-1' // for my redirect
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)], 
  exports: [RouterModule]
})
export class AppRoutingModule { }

子路由文件示例

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { YourComponent } from './components/yourComponent.component';

const routes: Routes = [
  {
    path: ':type/firstPage-1', // the route where i redirect first
    component: YourComponent
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)], 
  exports: [RouterModule]
})
export class YourComponentRoutingModule { }

对于模块,您将把您的特定组件routing.ts文件放入他的模块中,然后您将必须在app.module中导入此特定模块

希望这有帮助