[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:

Github

时间: 2024-08-05 11:12:23

[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 of Angular modules in large applications. 'use strict'; // Declare app level module which depends on filters, and services var App = angular.module('

[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. In Erge loading, it is recommended to create a shell component, inside shell component you need to define the router-outlet for each Auxiliary routes

[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 application while using the Angular 2 router. We are going to learn how we can prove that the memory leak is happening, we are going to learn what is causing

[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 non-critical routes is one of the points of the PRPL pattern. This lesson will show you how you can use the dynamic import to lazy load non-critical

Lazy Load, 延迟加载图片的 jQuery 插件 - NeoEase

body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI",Tahoma,Helvetica,Sans-Serif,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLiU,serif; font-size: 10.5pt; line-height: 1.5;

Entity中Lazy Load的属性序列化JSON时报错

The server encountered an internal error that prevented it from fulfilling this request.org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: com.party.dinner.entit

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

本文翻译自 Lazy Load Plugin for jQuery, 介绍一个 jQuery 插件, 它提供懒汉式加载页面图片的功能. Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预加载的处理方式正好是相反的. 在包含很多大图片长页面中延迟加载图片可以加快页面加载速度. 浏览器将会在加载可见图片之后即进入就绪状态. 在某些情况下还可以帮助降低服

Need a code of lazy load for div--reference

1. For all DIVs of a page $(function() {  $("div").lazyload({effect: 'fadeIn'});}); 2. For a particular DIV having some ID like: <div id="lazyload"> some content </div> $(function() {  $("div#lazyload").lazyload({

Lazy Load延迟加载图片效果

前些日子自己想搞个延时加载的玩玩,但js自己是不会写的,只有上网找插件了.在网上找了好多,都比较坑爹!为什么呢?大部分文章都是他妹的一篇不停的转载,这地方省一点那地方省一点.你说你转载就算了,保留原出处链接也行啊,这样也方便大家可以有更大的收获.但事实上是什么样子,我也就不多说了,我相信上网查过资料的亲都有过感受.在网上找了好久,最终还是有收获的.自己弄了几个小时后,终于折腾出来了.下面献上自己整理的源码,大神喷时还请手留情.先附一张效果图: css样式: .lazy{width:400px;h