Angular 7 嵌套模块路由
创始人
2024-10-16 19:36:11
0

要在Angular 7中创建嵌套模块路由,可以按照以下步骤进行操作:

  1. 创建父模块和子模块:

    • 创建父模块:ng generate module parent-module
    • 创建子模块:ng generate module child-module
  2. 在父模块中定义路由:

    • 在父模块的路由配置文件(parent-module-routing.module.ts)中,导入子模块并定义子模块的路由:

      import { NgModule } from '@angular/core';
      import { Routes, RouterModule } from '@angular/router';
      import { ChildModule } from '../child-module/child-module.module';
      
      const routes: Routes = [
        { path: '', loadChildren: () => ChildModule }
      ];
      
      @NgModule({
        imports: [RouterModule.forChild(routes)],
        exports: [RouterModule]
      })
      export class ParentModuleRoutingModule { }
      
  3. 在子模块中定义路由:

    • 在子模块的路由配置文件(child-module-routing.module.ts)中,定义子模块的路由:

      import { NgModule } from '@angular/core';
      import { Routes, RouterModule } from '@angular/router';
      import { ChildComponent } from './child.component';
      
      const routes: Routes = [
        { path: '', component: ChildComponent }
      ];
      
      @NgModule({
        imports: [RouterModule.forChild(routes)],
        exports: [RouterModule]
      })
      export class ChildModuleRoutingModule { }
      
  4. 在父模块和子模块中定义组件:

    • 在父模块中创建父组件(parent.component.ts、parent.component.html)。
    • 在子模块中创建子组件(child.component.ts、child.component.html)。
  5. 在父模块和子模块中导入路由模块:

    • 在父模块(parent-module.module.ts)中导入父模块的路由模块:

      import { NgModule } from '@angular/core';
      import { CommonModule } from '@angular/common';
      import { ParentComponent } from './parent.component';
      import { ParentModuleRoutingModule } from './parent-module-routing.module';
      
      @NgModule({
        declarations: [ParentComponent],
        imports: [
          CommonModule,
          ParentModuleRoutingModule
        ]
      })
      export class ParentModule { }
      
    • 在子模块(child-module.module.ts)中导入子模块的路由模块:

      import { NgModule } from '@angular/core';
      import { CommonModule } from '@angular/common';
      import { ChildComponent } from './child.component';
      import { ChildModuleRoutingModule } from './child-module-routing.module';
      
      @NgModule({
        declarations: [ChildComponent],
        imports: [
          CommonModule,
          ChildModuleRoutingModule
        ]
      })
      export class ChildModule { }
      
  6. 在根模块中配置路由:

    • 在根模块(app.module.ts)中导入父模块的路由模块:

      import { NgModule } from '@angular/core';
      import { BrowserModule } from '@angular/platform-browser';
      import { AppRoutingModule } from './app-routing.module';
      import { AppComponent } from './app.component';
      import { ParentModule } from './parent-module/parent-module.module';
      
      @NgModule({
        declarations: [AppComponent],
        imports: [
          BrowserModule,
          AppRoutingModule,
          ParentModule
        ],
        providers: [],
        bootstrap: [AppComponent]
      })
      export class AppModule { }
      

这样,你就可以在父组件的模板中使用来显示子组件了。在父组件的路由路径中,可以使用子组件的路径进行导航。

希望这个解决方案能帮到你!

相关内容

热门资讯

第六分钟辅助!随意玩免费辅助器... 第六分钟辅助!随意玩免费辅助器,总是真的是有辅助插件(有挂技术)1、玩家可以在随意玩免费辅助器透视最...
7分钟辅助!广东雀伸辅助器,切... 7分钟辅助!广东雀伸辅助器,切实是真的有辅助工具(有挂细节)1、首先打开广东雀伸辅助器辅助器下载最新...
5分钟辅助!蜀山四川破解版安卓... 5分钟辅助!蜀山四川破解版安卓,其实是真的有辅助技巧(有挂助手)一、蜀山四川破解版安卓游戏安装教程牌...
1分钟辅助!途游手游辅助脚本平... 1分钟辅助!途游手游辅助脚本平台,总是真的有辅助器(有挂工具)1、任何途游手游辅助脚本平台透视是真的...
两分钟辅助!闲娱江西打盾插件,... 两分钟辅助!闲娱江西打盾插件,原来是有辅助工具(揭秘有挂)1、金币登录送、破产送、升级送、活动送。详...
六分钟辅助!财神辅助功能,确实... 六分钟辅助!财神辅助功能,确实有辅助脚本(有挂教程)1、财神辅助功能辅助器安装包、财神辅助功能辅助器...
第6分钟辅助!闲来软件辅助,一... 第6分钟辅助!闲来软件辅助,一贯有辅助脚本(有挂详情)1、每一步都需要思考,不同水平的挑战会更加具有...
第一分钟辅助!摆八张辅助,本来... 第一分钟辅助!摆八张辅助,本来真的有辅助软件(的确有挂)暗藏猫腻,小编详细说明摆八张辅助破解器有用吗...
6分钟辅助!潮汕激k辅助软件,... 您好,潮汕激k辅助软件这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩...
第十分钟辅助!新道游app辅助... 第十分钟辅助!新道游app辅助器,竟然真的是有辅助器(有挂细节)1)新道游app辅助器辅助插件:进一...