zoukankan      html  css  js  c++  java
  • [Angular 8] Implement a Custom Preloading Strategy with Angular

    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:

    import { PreloadingStrategy, Route } from '@angular/router';
    import { Observable, of } from 'rxjs';
    import { Injectable } from '@angular/core';
    
    @Injectable({
      providedIn: 'root'
    })
    export class CustomPreloader implements PreloadingStrategy {
      preload(route: Route, load: Function): Observable<any> {
        if (route.data && route.data['preload']) {
          return load();
        } else {
          return of(null);
        }
      }
    }
    import { CustomPreloader } from './custom-preloader';
    
    @NgModule({
      declarations: [AppComponent, HomeComponent],
      imports: [
        BrowserModule,
        MatSidenavModule,
        BrowserAnimationsModule,
        RouterModule.forRoot(
          [
            {
              path: '',
              component: HomeComponent
            },
            {
              path: 'nyan',
              loadChildren: () =>
                import('./nyan/nyan.module').then(m => m.NyanModule),
              data: {
                preload: true
              }
            },
            {
              path: 'about',
              loadChildren: () =>
                import('./about/about.module').then(m => m.AboutModule)
            }
          ],
          {
            preloadingStrategy: CustomPreloader //PreloadAllModules
          }
        )
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule {}
  • 相关阅读:
    Python参考资料汇总
    Redis发布/订阅
    Redis读书笔记之API的理解和使用
    三、Dubbo源码学习笔记(一)之 Spring容器启动
    利用VMware在虚拟机上安装Zookeeper集群
    二、Dubbo相关文献链接
    一、Dubbo初体验
    @Retention小记
    EasyUI知识点杂记
    ---Mybatis3学习笔记(2)补充
  • 原文地址:https://www.cnblogs.com/Answer1215/p/11430757.html
Copyright © 2011-2022 走看看