Angular 2 lazy loading is a core feature of Angular 2. Lazy loading allows your application to start up faster because it only needs to use the main App Module when the page initially loads. As you navigate between routes, it will load the additional modules as you define them in your routes. Make sure to properly organize your files into Angular 2’s module pattern so that files that belong to each module don’t get required by other modules.

First we need to arrange the component into different module.

Create Four files

  • home.module.ts
  • home.routes.ts
  • contact.module.ts
  • contact.routes.ts

Each modue.ts will the entery file for this module, Angualr2 lazy loads the module in by router.

app.routes.ts:

import {RouterModule} from "@angular/router";
const routes = [
{path: '', loadChildren: 'app/home/home.module'},
{path: 'contact', loadChildren: 'app/contact/contact.module'},
]; export default RouterModule.forRoot(routes);

Here we use 'loadChildren' instead of 'component'. This helps lazy loading, to deduce the bundle size.

Also we point to the location of the module file for each component.

'forRoot': because app.routes.ts is the main entery point, for each child module, we will use 'forChild'

home.routes.ts:

import {HomeComponent} from "./home.component";
import {RouterModule} from "@angular/router";
const routes = [
{path: '', component: HomeComponent}
];
export default RouterModule.forChild(routes);

home.module.ts:

import {NgModule} from "@angular/core";
import {CommonModule} from "@angular/common";
import {HomeComponent} from "./home.component";
import {SearchBarComponent} from "./search-bar/search-bar.component";
import {ResultListComponent} from "./result-list/result-list.component";
import homeRoutes from './home.routes';
@NgModule({
imports: [
CommonModule,
homeRoutes
],
declarations: [HomeComponent, SearchBarComponent, ResultListComponent],
exports: [HomeComponent, SearchBarComponent, ResultListComponent]
}) export default class HomeModule{}

In module file, we import routes.

The same partten for contact component. To prove that, when we click the contact routeLink, we can see from the Devtool Netwrok panel, it load the contact module:

Github

[Angular2 Router] Lazy Load Angular 2 Modules with the Router的更多相关文章

  1. [AngularJS] Lazy loading Angular modules with ocLazyLoad

    With the ocLazyLoad you can load AngularJS modules on demand. This is very handy for runtime loading ...

  2. [Angular Router] Lazy loading Module with Auxiliary router

    Found the way to handle Auxiliary router for lazy loading moudle and erge load module are different. ...

  3. [Angular2 Router] Exiting an Angular 2 Route - How To Prevent Memory Leaks

    In this tutorial we are going to learn how we can accidentally creating memory leaks in our applicat ...

  4. [Angular] Lazy Load CSS at runtime with the Angular CLI

    Ever had the need for multiple "app themes", or even to completely dynamically load CSS ba ...

  5. [Vuex] Lazy Load a Vuex Module at Runtime using TypeScript

    Sometimes we need to create modules at runtime, for example depending on a condition. We could even ...

  6. [Vue] Lazy Load a Route by using the Dynamic Import in Vue.js

    By default, vue-router doesn’t lazy load the routes unless you tell it to do it. Lazy loading of the ...

  7. Lazy Load, 延迟加载图片的 jQuery 插件.

    Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...

  8. jQuery延迟加载插件(Lazy Load)详解

    最 新版本的Lazy Load并不能替代你的网页.即便你使用JavaScript移除了图片的src属性,有些现代的浏览器仍然会加载图片.现在你必须修改你的html代 码,使用占位图片作为img标签的s ...

  9. D:/apache2/conf/httpd.conf:Cannot load D:/apache2/modules/mod_actions.so

    报错如下: errors reported here must be corrected before service can be started.httpd:Syntax error on lin ...

随机推荐

  1. win7 提示"Windows 无法连接到System Event Notification Service服务......"的解决办法

    登录win7系统,突然出现如图1的提示,无线网络中断,不能上网,多次插拔无线网卡问题依然. 图1 解决过程如下: 1.检查网卡硬件状况,在设备管理器中查看网卡现象正常,排除网卡硬件故障. 2.查看服务 ...

  2. 大数据时代的数据存储,非关系型数据库MongoDB

    在过去的很长一段时间中,关系型数据库(Relational Database Management System)一直是最主流的数据库解决方案,他运用真实世界中事物与关系来解释数据库中抽象的数据架构. ...

  3. Python cookbook-读书笔记01

    1 数据结构和算法 1.1 Unpacking a sequence into separate variable(解包,赋值) >>> data = [ 'ACME', 50, 9 ...

  4. 微信企业支付--遇到不明确结果的err_code:SYSTEMERROR,NOT_FOUND

    前提 项目开发中实现微信提现的功能.使用到了两个接口 企业付款接口:https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfer ...

  5. 企业部署Linux应用将拥有更低的整体拥有成本

    企业部署Linux应用将拥有更低的整体拥有成本     使用Linux能为企业的IT解决方案降低TCO(整体拥有成本Total Cost of The Ownership)吗?在面临这个问题时,很多企 ...

  6. (window)Android Studio安装以及Fetching android sdk component information超时的解决方案

    转自:http://www.cnblogs.com/sonyi/p/4154797.html 在经过两年的开发之本后,Google 公司终于发布了 Android Studio 1.0,喜欢折腾的童鞋 ...

  7. 【JSON】JSON字符串的操作(不断积累中)

    一.JS遍历JSON串 示例01 目标:返回的JSON串中,一个Key对应的Value是一个数组(若在Java程序中,是一个List). 现在要求获取每个数组中的第一个对象元素中的name属性的值. ...

  8. 一、 使用存储过程实现数据分页(Sql Server 2008 R2)

    1.废话不多说了,直接上代码.调用这个存储过程只需要传递 表名,排序字段,搜索字段,以及页码,页码数量,搜索值(可空) create PROCEDURE NewPage --通用的分页存储过程,百万数 ...

  9. HD2043猜密码

    密码 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission ...

  10. java之四大皆空

    SE中的所有空的情况: 第一空:定义变量,变量没有值不能使用,不能打印 public class DiDaJieKong01 { public static void main(String[] ar ...