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 { }
      

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

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

相关内容

热门资讯

科技分享!德普之星私人局辅助器... 科技分享!德普之星私人局辅助器,pokernow辅助控制,可靠技巧(有挂教程);人气非常高,ai更新...
大神推荐!we-poker辅助... 自定义wepoker一直输的号能继续打吗系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑...
必知教程!sohoo竞技联盟辅... 1、必知教程!sohoo竞技联盟辅助器,wejoker辅助软件价格,安装教程(有挂方法);详细教程。...
如何分辨真伪!wepoker私... 如何分辨真伪!wepoker私人局可以透视,wepoker底牌透视脚本,AI教程(有挂教程)1、点击...
玩家必备教程!wepoker有... 玩家必备教程!wepoker有辅助功能吗,wepoker数据分析工具,wpk教程(有挂技巧);wep...
揭秘攻略!wepoker亲友圈... 揭秘攻略!wepoker亲友圈有用吗,wepoker透视app下载,可靠教程(有挂透明);1.wep...
一分钟揭秘!wepoker插件... 一分钟揭秘!wepoker插件功能辅助器,fishpoker透视,技巧教程(有挂技巧)是由北京得we...
分享实测!wepoker软件靠... 分享实测!wepoker软件靠谱么,pokemmo免费脚本,力荐教程(有挂神器);建议优先通过wep...
一分钟教你!wepoker手机... 一分钟教你!wepoker手机版透视脚本,wepoker辅助器激活码,新版2025教程(有挂方法)是...
分享一款!wepoker有没有... 分享一款!wepoker有没有机器人,wepoker透视是真的吗,透明教程(有挂透明)是一款可以让一...