angular如何处理多模块间的路由问题,模块1中如何使用模块2中的组件

一、需求&问题&版本

angular版本:angular 9

需求:

​ 1.将项目分成login,app-content,app-component等多模块

​ 2.多模块间的路由如何配置,模块间路由如何跳转

​ 3.模块1中如何使用模块2中的组件

项目架构
在这里插入图片描述

二、如何处理多模块多路由

重点:

1.模块的子路由的设置RouterModule.forChild()

2.根路由的引入子路由

1.根路由

跟路由中引入多个模块的子路由,确保跨模块的路由可相互跳转

@filename(app-routing.module)

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  // 2.配置默认路由
  { path: '', pathMatch: 'full', redirectTo: 'page' },
  // 1.引入子模块的路由
  { path: 'login', loadChildren: () => import('./app-login/app-login.module').then(m => m.AppLoginModule) },
  { path: 'page', loadChildren: () => import('./app-content/app-content.module').then(m => m.AppContentModule) },
];

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

2.login模块的子路由设置

@filename(app-login.module)

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from '@angular/router';
import { AppLoginComponent } from './app-login.component';
import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';

const routes: Routes = [
  {
    path: '', component: AppLoginComponent, children: [
      { path: '', redirectTo: 'login', pathMatch: 'prefix' },
      { path: 'login', component: LoginComponent },
      { path: 'register', component: RegisterComponent },
    ]
  }
]

@NgModule({
  declarations: [AppLoginComponent, LoginComponent, RegisterComponent],
  imports: [
    RouterModule.forChild(routes),
    CommonModule
  ]
})
export class AppLoginModule { }

3.app-content模块的子路由设置

import { NgModule } from '@angular/core';
import { CommonModule } from '@angularcommon';
// 1.引入依赖
import { RouterModule, Routes } from '@angular/router';

import { AppContentComponent } from './app-content.component';
import { HomeComponent } from './home/home.component';
import { DashboardComponent } from './home/dashboard/dashboard.component';
import { OrderComponent } from './order/order.component';
import { OrderListComponent } from './order/order-list/order-list.component';

// 2. 设置路由
const routes: Routes = [
  {
    path: '', component: AppContentComponent, children: [
      { path: '', redirectTo: 'home', pathMatch: 'prefix' },
      {
          path: 'home', component: HomeComponent, children: [
              { path: '', redirectTo: 'dashboard', pathMatch: 'prefix' },
              { path: 'dashboard', component: DashboardComponent },
          ]
      },
    ]
  }
]

@NgModule({
  declarations: [AppContentComponent, HomeComponent, DashboardComponent, OrderComponent, OrderListComponent],
  imports: [
    RouterModule.forChild(routes), // 3.设置为子路由
    CommonModule
  ]
})
export class AppContentModule { }

三、如何在模块1的组件中使用模块2中的组件

例子

app-content模块中的app-content组件使用app-component模块中的header组件

1. 模块2中导出组件

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HeaderComponent } from './header/header.component';
import { SiderComponent } from './sider/sider.component';

const declarationsArr = [HeaderComponent, SiderComponent]

@NgModule({
  declarations: declarationsArr,
  imports: [
    CommonModule
  ],
  exports: declarationsArr // 重点:在这里导出
})
export class AppComponentModule { }

2.将模块2导入模块1

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Routes, RouterModule } from '@angular/router';
// 导入1
import { AppComponentModule } from '../app-component/app-component.module';
const routes: Routes = [ // ...];

@NgModule({
  declarations: [ // ...],
  imports: [
    RouterModule.forChild(routes),
    CommonModule,
    // 导入2
    AppComponentModule,
  ]
})
export class AppContentModule { }

3.模块1的组件使用模块2的组件

<app-header></app-header>
<router-outlet></router-outlet>

参考1:

https://blog.csdn.net/wrs120/article/details/78612692/?utm_term=angular6%E8%B7%B3%E8%BD%AC&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2allsobaiduweb~default-0-78612692&spm=3001.4430

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 如果要在 Angular 调用懒加载模块里的路由,你需要在根路由模块使用 `loadChildren` 属性来配置懒加载。这个属性可以接受一个模块的路径字符串,并返回一个懒加载函数。 下面是一个示例: ``` const routes: Routes = [ { path: 'lazy', loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule) } ]; ``` 然后,你可以在懒加载模块里的路由模块配置你的路由。 ``` const routes: Routes = [ { path: '', component: LazyComponent } ]; @NgModule({ imports: [RouterModule.forChild(routes)], exports: [RouterModule] }) export class LazyRoutingModule { } ``` 现在,你就可以通过访问 `/lazy` 来访问这个懒加载模块里的路由了。 ### 回答2: 在Angular使用懒加载模块可以提高应用的性能和加载速度。当需要使用到某个模块时,可以通过懒加载来按需加载该模块,而不是在应用启动时一次性加载全部模块。 要调用懒加载模块里的路由,首先需要在主模块路由配置定义懒加载路由。在定义路由时,路径需要指定懒加载的模块的路径,并通过loadChildren属性来加载模块。例如: ```typescript const routes: Routes = [ { path: 'lazy', loadChildren: 'app/lazy/lazy.module#LazyModule' } ]; ``` 这样在访问路径为"/lazy"时,会触发懒加载加载LazyModule模块。接下来,我们可以在懒加载的模块定义自己的子路由。例如: ```typescript const routes: Routes = [ { path: '', component: LazyComponent }, { path: 'details', component: DetailsComponent } ]; ``` 这样就可以通过"/lazy/details"访问到懒加载模块的DetailsComponent组件了。 在应用调用懒加载模块里的路由时,可以使用routerLink指令或者调用Router服务的navigate方法。例如: ```html <a [routerLink]="['/lazy']">Lazy Module</a> <a [routerLink]="['/lazy/details']">Details</a> ``` 或者在组件通过Router服务的navigate方法来实现路由跳转: ```typescript import { Router } from '@angular/router'; @Component({...}) export class MyComponent { constructor(private router: Router) {} goToLazyModule() { this.router.navigate(['/lazy']); } goToDetails() { this.router.navigate(['/lazy/details']); } } ``` 以上就是如何在Angular调用懒加载模块里的路由的方法。通过懒加载可以按需加载模块路由,从而提高应用的性能和加载速度。 ### 回答3: Angular的懒加载是一种优化技术,它允许将模块按需加载,而不是在应用启动时一次性加载所有模块。 调用懒加载模块里的路由是通过在应用定义路由并配置懒加载模块的路径来实现的。当用户访问到该路由时,Angular会根据配置的懒加载路径动态加载该模块,并且渲染出对应的组件。 要实现这个功能,我们需要在应用的路由配置文件指定需要懒加载的模块的路径。例如,假设我们有一个名为`LazyModule`的模块,我们可以在路由配置文件如下定义该模块路由: ```typescript const routes: Routes = [ { path: 'lazy', loadChildren: () => import('./lazy.module').then(m => m.LazyModule) } ]; ``` 上述代码,`loadChildren`属性指定了懒加载模块的路径,使用`import().then()`的语法来动态加载该模块,并返回一个`Promise`对象。 当用户访问到路径`/lazy`时,Angular会自动加载`LazyModule`模块,并根据该模块路由配置渲染对应的组件。 总结起来,要调用懒加载模块里的路由,我们需要在应用的路由配置文件定义懒加载模块的路径,并在访问对应的路由时,Angular会自动加载该模块并渲染组件。这样可以提高应用的初始加载速度和性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值