Preloading all modules is quite an extreme approach and might not always be desirable. For instance, you don't want to preload lazy routes a user might not even have access to. Therefore, in this lesson we're going to have a look at how to define a custom preloading strategy in Angular.

custom-preloader.ts:

  1. import { PreloadingStrategy, Route } from '@angular/router';
  2. import { Observable, of } from 'rxjs';
  3. import { Injectable } from '@angular/core';
  4.  
  5. @Injectable({
  6. providedIn: 'root'
  7. })
  8. export class CustomPreloader implements PreloadingStrategy {
  9. preload(route: Route, load: Function): Observable<any> {
  10. if (route.data && route.data['preload']) {
  11. return load();
  12. } else {
  13. return of(null);
  14. }
  15. }
  16. }
  1. import { CustomPreloader } from './custom-preloader';
  2.  
  3. @NgModule({
  4. declarations: [AppComponent, HomeComponent],
  5. imports: [
  6. BrowserModule,
  7. MatSidenavModule,
  8. BrowserAnimationsModule,
  9. RouterModule.forRoot(
  10. [
  11. {
  12. path: '',
  13. component: HomeComponent
  14. },
  15. {
  16. path: 'nyan',
  17. loadChildren: () =>
  18. import('./nyan/nyan.module').then(m => m.NyanModule),
  19. data: {
  20. preload: true
  21. }
  22. },
  23. {
  24. path: 'about',
  25. loadChildren: () =>
  26. import('./about/about.module').then(m => m.AboutModule)
  27. }
  28. ],
  29. {
  30. preloadingStrategy: CustomPreloader //PreloadAllModules
  31. }
  32. )
  33. ],
  34. providers: [],
  35. bootstrap: [AppComponent]
  36. })
  37. export class AppModule {}

[Angular 8] Implement a Custom Preloading Strategy with Angular的更多相关文章

  1. How to: Implement a Custom Base Persistent Class 如何:实现自定义持久化基类

    XAF ships with the Business Class Library that contains a number of persistent classes ready for use ...

  2. angular的跨域(angular百度下拉提示模拟)和angular选项卡

    1.angular中$http的服务: $http.get(url,{params:{参数}}).success().error(); $http.post(url,{params:{参数}}).su ...

  3. angular 2+ 变化检测系列三(Zone.js在Angular中的应用)

    在系列一中,我们提到Zone.js,Zones是一种执行上下文,它允许我们设置钩子函数在我们的异步任务的开始位置和结束位置,Angular正是利用了这一特性从而实现了变更检测. Zones.js非常适 ...

  4. 【Angular JS】正确调用JQuery与Angular JS脚本 - 修复Warning: Tired to load angular more than once

    自己正在做一个小网站,使用Angular JS + Express JS + Mongo DB,在开发过程中,遇到一些问题,所以整理出来.希望对大家都有帮助. 这是今天解决的一个问题,Angular ...

  5. [Angular] Implement a custom form component by using control value accessor

    We have a form component: <label> <h3>Type</h3> <workout-type formControlName=& ...

  6. [Angular Directive] Implement Structural Directive Data Binding with Context in Angular

    Just like in *ngFor, you're able to pass in data into your own structural directives. This is done b ...

  7. How to implement a custom type for NHibernate property

    http://blog.miraclespain.com/archive/2008/Mar-18.html <?xml version="1.0" encoding=&quo ...

  8. [Angular 2] Filter items with a custom search Pipe in Angular 2

    This lessons implements the Search Pipe with a new SearchBox component so you can search through eac ...

  9. How to implement a custom PropertyEditor so that it supports Appearance rules provided by the ConditionalAppearance module

    https://www.devexpress.com/Support/Center/Question/Details/T505528/how-to-implement-a-custom-propert ...

随机推荐

  1. java多线程中篇(一) —— Thread详情

    简介 简言之,现在的JDK线程模型基于操作系统原生线程,所以模型依赖于操作系统对线程的支持,另外Windows和Linux系统提供的线程模型就是一对一的 所以可以简单认为: 现在Java线程与操作系统 ...

  2. Web服务器和Tomcat

    Web服务器常用: WebLogic:是BEA公司的推出的产品,现在已经被oracle收购,是目前应用最广泛的Web服务器,支持JavaEE规范,商用收费,开发者可以免费使用. WebSphere:I ...

  3. Redis--list类型操作命令

    列表 list Redis列表是简单的字符串列表,按照插入顺序排序.你可以添加一个元素导列 表的头部(左边)或者尾部(右边) 列表 list——基本命令 lpush 语法:lpush key valu ...

  4. 如何在mongoengine中使用referencefield引用本类

    引用:原文 from mongoengine import * class Employee(Document): name = StringField() boss = ReferenceField ...

  5. PB笔记之验证必填(pfc_validation)

    pfc_validation事件中可以在保存时进行提示

  6. SPA中使用jwt

    什么是jwt? JSON Web Token (JWT),它是目前最流行的跨域身份验证解决方案 JWT的工作原理 1. 是在服务器身份验证之后,将生成一个JSON对象并将其发送回用户,示例如下:{&q ...

  7. nodejs模块fs——文件操作api

    // fs模块常用api // 读取文件 .写入文件 .追加文件. 拷贝文件 .删除文件 // 读取文件 // fs.readFile(path[, options], callback) // fs ...

  8. hdu 3265 第一类斯特林数

    先和第二类做一个对比 第一类Stirling数是有正负的,其绝对值是包含n个元素的集合分作k个环排列的方法数目.递推公式为, S(n,0) = 0, S(1,1) = 1. S(n+1,k) = S( ...

  9. GOF学习笔记1:术语

    1.abstract class 抽象类定义了一个接口,把部分或全部实现留给了子类,不能实例化. 2.abstract coupling  抽象耦合如果一个类A引用了另一个抽象类B,那么就说A是抽象耦 ...

  10. POJ3255(Roadblocks)--次短路径

    点这里看题目 3228K 485MS G++ 2453B 根据题意和测试用例知道这是一个求次短路径的题目.次短路径,就是比最短路径长那么一丢丢的路径,而题中又是要求从一点到指定点的次短路径,果断Dij ...