本文出自最三方Angular系列教程,转载请注明出处!
前言
Lazyloading是指从一个资料物件通过方法获得里面的一个属性物件时,这个对应物件实际并没有随其父资料物件创建时一起储存在执行空间中,而是在其读取方法第一次被呼叫时才从其他资料来源中加载到执行空间中,这样可以避免过早地汇入过大的资料物件但并没有使用的空间占用浪费。
Lazyloading在加载非常多不同内容时非常有感。当一个网站包含的东西越多,打开的速度也会越慢,当网页从上到下读取,当遇到了比较大的元件时,读取速度上绝不会像读取小元件一样顺畅,常常因为所含的组件太多,导致网站读取速度变慢,这网站真的是吃了闷亏。那是否有办法改善这种因为读取速度而造成网站打开太慢的问题呢?
实现
Angular2 中 , lazy loading 已经被建在 Router 里了。 接下来要花几个步骤:
- 指定 app.routing.ts 中要 lazy load 的路由 (routes)
- 创建 Component 路由文件
- 调整要被 lazy load 的 module 的内容
路径物件
为了区分 lazy loading,我们要创建一个路径物件包含 loadChildren 特性。在这物件里面要设定 module 的名称和路径。
03 | import {Routes, RouterModule} from "@angular/router" ; |
04 | import {ModuleWithProviders} from "@angular/core" ; |
06 | const appRoutes: Routes = [ |
08 | { path: "" , redirectTo: "home" , pathMatch: "full" }, |
10 | // 要处理的 Product APP 页面 |
11 | { path: "product" , loadChildren: "app/product/product.module#ProductModule" } |
14 | export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes); |
|
这样代表 URL 会从 /product 开始, angular 路由将会得到 module 文件 app/product/product.module 并预期有 ProductModule 这个 class。
路由 Module
01 | //product/product.routing.ts |
03 | import {RouterModule} from "@angular/router" ; |
04 | import {ModuleWithProviders} from "@angular/core" ; |
05 | import {ProductComponent} from "./product.component" ; |
07 | import {ProductsListComponent} from "./products-list.component" ; |
08 | import {ProductDetailsComponent} from "./product-details.component" ; |
13 | component: ProductComponent, |
16 | {path: "" , component: ProductsListComponent}, |
17 | {path: ":id" , component: ProductDetailsComponent} |
22 | export const routing: ModuleWithProviders = RouterModule.forChild(routes); |
|
设定 Component
定义 ProductComponent
01 | //product/product.component.ts |
02 | import {Component} from "@angular/core" ; |
08 | <router-outlet></router-outlet> |
11 | export class ProductComponent {} |
|
router-outlet 会呈现这个 module 内容
有 Lazy Load 的 Module
01 | //product/product.module.ts |
03 | import {NgModule} from "@angular/core" ; |
04 | import {ProductsService} from "./products.service" ; |
05 | import {ProductsListComponent} from "./products-list.component" ; |
06 | import {ProductDetailsComponent} from "./product-details.component" ; |
07 | import {routing} from "./product.routing" ; |
08 | import {ProductComponent} from "./product.component" ; |
11 | declarations: [ProductComponent, ProductsListComponent, ProductDetailsComponent], |
13 | providers: [ProductsService], |
15 | export class ProductModule {} |
|
这边会用到 product.routing 的 routing。
测试
假设你的网站有三个 Module: home project about 那加入 lazy loading 的前后差异大概会是这样
原因是因为加入 lazy loading 所以会先只加载 home 根 about,在这边我们假设他们两个非常轻量,那就会使得整个网页加载的顺畅度整体提升,而比较大的 project 则在之后加载,就不会拖垮速度了!
补充:模板加载组件
方式
假设有两个 Components fast & slow
01 | //src/components/fast.ts |
02 | import { Component } from '@angular/core' ; |
11 | export class FastComponent { } |
12 | //src/components/slow.ts |
13 | import { Component } from '@angular/core' ; |
22 | export class SlowComponent { } |
|
在 Template 里面加载 Component 是最常用加载方式。非常简单,只需要存放相应的 selector ,模板发动机就会自己找到并编译。
02 | import {Component} from '@angular/core' |
04 | import {FastComponent} from 'src/components/fast' |
05 | import {SlowComponent} from 'src/components/slow' |
08 | selector: 'template-load' , |
10 | <h1>方式一:template 动态加载</h1> |
14 | directives: [ FastComponent, SlowComponent ] |
16 | export class TemplateLoadComponent { |
|
最三方Angular2系列教程列表
- [Day01]Angular 2教程:01 简介
- [Day02]Angular 2教程:02 Angular 2 的灵魂
- [Day03]Angular 2教程:03 当个好的建筑师之Angular2架构
- [Day04]Angular 2教程:04 注入依赖
- [Day05]Angular 2教程:05 模板与绑定--互动的根本
- [Day06]Angular 2教程:06 Angular 2 表单
- [Day07]Angular 2教程:07 Angular 2 PIPES(通道)
- [Day08]Angular 2教程:08 Lazy Loading 别让载入速度拖垮
- [Day09]Angular 2教程:09 Angular 2 路由
- [Day10]Angular 2教程:10 Angular 2 动画
- [Day11]Angular 2教程:11 HTTP 方法--让我Call API
- [Day12]Angular 2教程:12 Angular 2 多语言 ( 1 )
- [Day13]Angular 2教程:13 Angular 2 多语言 ( 2 )
- [Day14]Angular 2教程:14 Angular 2 + <ng-content> 嵌入式设计灵活组织HTML
- [Day15]Angular 2教程:15 Angular 2 Module ( 1 )
- [Day16]Angular 2教程:16 Angular 2 Module ( 2 )
- [Day17]Angular 2教程:17 Angular 2 替 Component 加上 CSS 的所有招数
- [Day18]Angular 2教程:18 Angular 2 组件继承 ( Component Inheritance )
- [Day19]Angular 2教程:19 Angular 2 Angular 2 Input(s) & Output(s) 傻傻分不清
- [Day20]Angular 2教程:20 Angular 2 组件相关的路径
- [Day21]Angular 2教程:21 Angular 2 + Ionic = Mobile App ( 1 ) 介绍
- [Day22]Angular 2教程:22 Angular 2 + Ionic = Mobile App ( 2 ) 架构
- [Day23]Angular 2教程:23 Angular 2 + Ionic = Mobile App ( 3 ) 实战 Todo List
- [Day24]Angular 2教程:24 Angular 2 + Ionic = Mobile App ( 4 ) 发布 App
- [Day25]Angular 2教程:25 Angular 2 预编译 Ahead-of-Time (AoT)
- [Day26]Angular 2教程:26 Angular 2 装饰器 @ContentChild
- [Day27]Angular 2教程:27 Angular 2 装饰器 @ViewChild
- [Day28]Angular 2教程:28 Angular 2 制作 tab 元件
- [Day29]Angular 2教程:29 Angular 2 @Directive
- [Day30]Angular 2教程:30 Angular 2 单元测试 Unit Test
- [Day31]Angular 2教程:31 总结 Angular 2 给初学者的学习指南