From router v3.1.0, we have preloading system with router.

PreloadAllModules

After the init module loaded, router will preloading the rest module at the background.

const indexRoute = {path: '', redirectTo: 'home', pathMatch: 'full'};
const fallbackRoute = {path: '**', component: NotFoundComponent};
const routes = [
{path: 'legacy-url', redirectTo: '/home', pathMatch: 'prefix'},
{path: 'home', loadChildren: 'app/home/home.module', name: 'Home'},
{path: 'heros', loadChildren: 'app/heros/heros.module', name: 'Heros'},
{path: 'contact', loadChildren: 'app/contact/contact.module', name: 'Contact'},
indexRoute,
fallbackRoute,
]; export default RouterModule.forRoot(routes, {
useHash: true,
preloadingStrategy: PreloadAllModules
});

Custom Preload Strategy:

const indexRoute = {path: '', redirectTo: 'home', pathMatch: 'full'};
const fallbackRoute = {path: '**', component: NotFoundComponent};
const routes = [
{path: 'legacy-url', redirectTo: '/home', pathMatch: 'prefix'},
{path: 'home', loadChildren: 'app/home/home.module', name: 'Home'},
{path: 'heros', loadChildren: 'app/heros/heros.module', name: 'Heros', data: {preload: true}},
{path: 'contact', loadChildren: 'app/contact/contact.module', name: 'Contact', data: {preload: true}},
{path: 'message', loadChildren: 'app/message/message.module', name: 'Message'},
indexRoute,
fallbackRoute,
]; export default RouterModule.forRoot(routes, {
useHash: true,
preloadingStrategy: PreloadSelectedModuledsList
//preloadingStrategy: PreloadAllModules
});

In the router config, we use :

data: {preload: true}

To tell which modue should be preloaded.

Then provide a custom loading strategy:

import {PreloadingStrategy, Route} from "@angular/router";
import {Observable} from "rxjs";
export class PreloadSelectedModuledsList implements PreloadingStrategy {
preload(route: Route, load: Function): Observable<any> {
return route.data.preload ? load() : Observable.of(null);
} }

Last step, in the app.module.ts, inject the provider to the providers list:

  providers: [
{provide: APP_BASE_HREF, useValue: '/'},
{
provide: API_URL,
useValue: `https://en.wikipedia.org/w/api.php?callback=JSONP_CALLBACK`
},
{
provide: STARWARS_BASE_URL,
useValue: `https://starwars-json-server-ewtdxbyfdz.now.sh`
},
PreloadSelectedModuledsList
],

Github

[Angular2 Router] Preload lzay loading modules的更多相关文章

  1. [Angular2 Router] Resolving route data in Angular 2

    From Article: RESOLVING ROUTE DATA IN ANGULAR 2 Github If you know Anuglar UI router, you must know ...

  2. [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 ...

  3. [AngularJS] Lazy Loading modules with ui-router and ocLazyLoad

    We've looked at lazy loading with ocLazyLoad previously, but what if we are using ui-router and want ...

  4. [Angular2 Router] Load Data Based on Angular 2 Route Params

    You can load resource based on the url using the a combination of ActivatedRouteand Angular 2’s Http ...

  5. Angular2 Router路由相关

    路由设置 Angular中路由的配置应该按照先具体路由到通用路由的设置,因为Angular使用先匹配者优先的原则. 示例: 路由设置如下: export const reportRoute: Rout ...

  6. [Angular2 Router] CanActivate Route Guard - An Example of An Asynchronous Route Guard

    In this tutorial we are going to learn how we can to configure an can activate route guard in the An ...

  7. [Angular2 Router] CanDeactivate Route Guard - How To Confirm If The User Wants To Exit A Route

    In this tutorial we are going to learn how we can to configure an exit guard in the Angular 2 Router ...

  8. [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 ...

  9. [Angular2 Router] Optional Route Query Parameters - The queryParams Directive and the Query Parameters Observable

    In this tutorial we are going to learn how to use the Angular 2 router to pass optional query parame ...

随机推荐

  1. 关于li的排列,我的面试题

    来到北京的第二周,收到了单位的面试,一面的时候面试官问了微信钱包里的那个快速入口的排列,我当时在面试官的引导下答的还可以,但是在实际中有很多的方法和各自不同的问题,我来总结下. 1.flex布局,其实 ...

  2. shell项目-分发系统-expect讲解

    shell项目-分发系统-expect讲解 yum install -y expect 1. 自动远程登录 #! /usr/bin/expect set host "192.168.133. ...

  3. dig---域名查询

    dig命令是常用的域名查询工具,可以用来测试域名系统工作是否正常. QUESTION SECTION 这部分是提问,显示你要查询的域名 ANSWER SECTION 即答案,显示查询到的域名对应的IP ...

  4. 【Codeforces Round #457 (Div. 2) C】Jamie and Interesting Graph

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找比n-1大的最小的素数x 1-2,2-3..(n-2)-(n-1)长度都为1 然后(n-1)-n长度为(x-(n-2)) 然后其他 ...

  5. Android eclipse 提示java代码 快捷键

    1.提示java代码能够用ALT+/ 键就能够了(前提是你要把你须要的类或方法的首字母打出来).我添加的部分:仅仅要输入sysout,然后alt+/键就能够输出System.out.prinln(), ...

  6. 玩转Bash脚本:选择结构之case

    总第5篇 之前,我们谈到了if. 这次我们来谈还有一种选择结构--case. case与if if用于选择的条件,不是非常多的情况,假设选择的条件太多.一系列的if.elif,.也是醉了. 没错,ca ...

  7. 7.Maven之(七)pom.xml配置文件详解

    转自:https://blog.csdn.net/qq_33363618/article/details/79438044 setting.xml主要用于配置maven的运行环境等一系列通用的属性,是 ...

  8. ELK之日志查询、收集与分析系统

    项目由来 (1)开发人员不能登录线上服务器查看详细日志,经过运维周转费时费力 (2)日志数据分散在多个系统,难以查找与整合 (3)日志数据量巨大,查询速度太慢,无法满足需求 (4)无法全局掌控项目运行 ...

  9. 进阶攻略|最全的前端开源JS框架和库

    新的 Javascript 库层出不穷,从而Web 社区愈发活跃.多样.在多方面快速发展.详细去描述每一种主流的 Javascript框架和库近乎不可能,所以在这篇文章中主要介绍一些对前端发展最具影响 ...

  10. JavaScript学习总结(4)——JavaScript数组

    JavaScript中的Array对象就是数组,首先是一个动态数组,无需预先制定大小,而且是一个像Java中数组.ArrayList.Hashtable等的超强综合体. 一.数组的声明 常规方式声明: ...