文档针对以下项目环境,借助webpack实现angular路由懒加载:

  • Jhipster 4.x
  • Anguar 2~5
  • Jhipster 5(生成项目时,admin module已实现异步路由的配置)
  • 基于webpack配置

开始正文:

1.安装依赖

npm install angular-router-loader --save-dev

2.添加loader

添加安装的依赖到 loader

// webpack.common.js 
module: {
            rules: [
                {
                    test: /\.ts$/,
                    loaders: [
                        'angular2-template-loader',
                        'awesome-typescript-loader',
                        'angular-router-loader' // loader
                    ],
                    ...

3.创建(或改造)异步路由模块

如创建Customer模块
Customer 模块不会被立即加载,直到用户路由到Customer页。这个模块有自己的组件和路由配置,加载的入口是customer.module.ts,代码如下:

// customer.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { CustomerRoutingModule } from './customer-routing.module';
import { CustomerComponent } from './customer.component';


@NgModule({
  imports: [
    CommonModule,
    CustomerRoutingModule
  ],
  declarations: [CustomerComponent]
})
export class CustomerModule { }
// customer-routing.module.ts
/*
* customer.component.ts 和 customer.component.html等亦可使用ng命令创建
*/
import { CustomerComponent } from './customer.component';
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  {
    path: '',
    component: CustomerComponent
  }
];

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

4.配置Angular路由

配置路由文件 Jhipster项目中可能是 layout-routing.module.ts (ng 生成的可能是 app-routing.module.ts)

路由配置方案1

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

const routes: Routes = [
  {
    path: 'customer',
    loadChildren: '../customer/customer.module#CustomerModule' // 异步路由加载模块,注:路径必须正确
  },
  {
    path: 'local', // 非异步路由
    component: LocalComponent
  }
];

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

路由配置方案2

在我的项目中曾经使用这种方式进行异步路由的配置,发现可能存在数据请求事件的问题。项目中遇到的场景是:把模块改造完成后,能正常异步加载路由,但由于业务复杂,加载业页时部分请求会重复(即首页请求了A,异步路由加载后会再次请求A),然而这部分请求没有被再次执行。因此使用下面的另一种方案进行异步路由配置。
import { LocalComponent } from './local/local.component';
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  {
     // 异步路由加载模块,注:路径必须正确
    path: 'customer', loadChildren: () => new Promise((resolve) => {
            (require as any).ensure([], (require) => {
                resolve(require('../customer/customer.module').CustomerModule);
            });
        })
  },
  {
    path: 'local', // 非异步路由
    component: LocalComponent
  }
];

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

5.配置module

配置 Angular app.module.ts

import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { LocalComponent } from './local/local.component';

@NgModule({
  declarations: [
    AppComponent,
    LocalComponent // 非异步路由组件
],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    AppRoutingModule // 异步路由模块
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

6.构建并运行

yarn startnpm run start

运行后,会编译出模块文件,当跳转到异步路由时,查看网络请求,即可看到跳转路由时会加载对应的js文件。如下图

clipboard.png

clipboard.png


Guoye
315 声望9 粉丝

引用和评论

0 条评论