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 {}
  • 相关阅读:
    js字符串截取函数slice()、substring()、substr()
    js获取字符串最后一位方法
    支持xhr浏览器:超时设定、加载事件、进度事件
    深入理解ajax系列第一篇——XHR对象
    MySQL命令行操作
    nodejs中mysql用法
    大衍数列
    牌型种数
    加法变乘法
    三羊献瑞
  • 原文地址:https://www.cnblogs.com/Answer1215/p/11430757.html
Copyright © 2011-2022 走看看