【问题标题】:How to create a nested route structure using separate routing.module.ts files per sub module如何使用每个子模块的单独 routing.module.ts 文件创建嵌套路由结构
【发布时间】:2018-01-12 15:56:18
【问题描述】:

我有一个应用,其路由结构如下

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

【问题讨论】:

  • 请不要使用angularX-somethig标签,将框架称为angular
  • @Jota.Toledo 没有这样的规则,因为应始终牢记标签描述。仍然有 angular-* 引用 AngularJS 的标签。 angular5 标签不相关,而 angular4-router 是; angular-router 是低调的,它所指的路由器不够具体(描述中的 service 词表明它甚至可能是 AngularJS 标签)。
  • “Angular” 应该严格用于 Angular 框架,“angularjs” 用于前者。遗憾的是,目前情况并非如此,但这并不意味着我们应该鼓励 angular2-、angular4- 或 angular5-* 标签,因为 IMO 制定特定的标签版本是没有意义的,该信息应该是在问题本身中提供。我同意你的观点,一些描述应该改进。

标签: angular typescript angular4-router


【解决方案1】:

我将发布我的示例:

应用路由

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 中导入这个特定的模块

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2021-06-25
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多