[Angular2 Router] Lazy Load Angular 2 Modules with the Router
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:

[Angular2 Router] Lazy Load Angular 2 Modules with the Router的更多相关文章
- [AngularJS] Lazy loading Angular modules with ocLazyLoad
With the ocLazyLoad you can load AngularJS modules on demand. This is very handy for runtime loading ...
- [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. ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- Lazy Load, 延迟加载图片的 jQuery 插件.
Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...
- jQuery延迟加载插件(Lazy Load)详解
最 新版本的Lazy Load并不能替代你的网页.即便你使用JavaScript移除了图片的src属性,有些现代的浏览器仍然会加载图片.现在你必须修改你的html代 码,使用占位图片作为img标签的s ...
- 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 ...
随机推荐
- 【Unity入门】场景、游戏物体和组件的概念
版权声明:本文为博主原创文章,转载请注明出处. 游戏和电影一样,是通过每一个镜头的串联来实现的,而这样的镜头我们称之为“场景”.一个游戏一般包含一个到多个场景,这些场景里面实现了不同的功能,把它们组合 ...
- ansible文件模块使用
1. 文件组装模块-assemble assemble主要是将多份配置文件组装为一份配置文件. 参数 必填 默认 选项 说明 Backup 否 No Yes/no 是否创建备份文件,使用时间戳 Del ...
- scala: How to write a simple HTTP GET request client in Scala (with a timeout)
Scala CookBook: http://scalacookbook.com/ @throws(classOf[java.io.IOException]) @throws(classOf[java ...
- docker集成管理工具-shipyard的开发环境搭建笔记
前段时间一直在研究openstack,后来老师告诉我需要用docker容器来搭建hadoop集群,所以就将战场转移到docker上来了,话说docker最近这段时间太火了,但是说实话我觉得应用起来还不 ...
- xcode 6.4 安装Alcatraz失败解决方法
Alcatraz Xcode6.4安装不了解决方法http://www.cocoachina.com/bbs/read.php?tid=310380 版权声明:本文为博主原创文章,未经博主允许不得转载 ...
- C++ 之关联容器 map
标准库定义了四种关联容器:map是其中之一(另外还有set.multimap.multiset).map的元素以键-值(key-value),在学了顺序容器之后,再学习关联容器,就比较比较好理解了. ...
- 用jstack工具分析java程序
最近做项目时遇到了一个问题,我的多个采集线程中,有一个线程经常挂起,线程并没有死掉,但是一直采集不到数据,为了解决这个问题,用到了jstack. 首先查找到java进程的pid,ps -ef|grep ...
- qq邮箱发送
454 Authentication failed, please open smtp flag first!用QQ邮箱测试报错 我用QQ邮箱测试javamail发送邮件的功能,用户名密码设置正确,却 ...
- POJ1837 Balance(DP)
POJ1837http://poj.org/problem?id=1837 题目大意就是说有一个称上有C个挂钩,告诉你每个挂钩的位置,现在有G个重物,求是之平衡的方法数. 转化一下:DP[i][j]表 ...
- 编译安装-Apache
一.配置选项说明 二.安装apache 1.环境准备 2.安装apr 3.安装apr-util 4.安装pcre 5.安装httpd 6.修改配置文件 7.开机自启动 8.注册为服务 9.测试 一.配 ...