• [Angular] Show a Loading Indicator for Lazy Routes in Angular


    We can easily code split and lazy load a route in Angular. However when the user then clicks that lazy loaded route, it make some time to actually fetch and run the JavaScript bundle. In this lesson we'll see how we can implement a loading indicator for such lazy loaded routes.

    <!-- app.component.thml -->
    
    <router-outlet>
      <span class="loader" *ngIf="loading"></span>
    </router-outlet>
    

     

    import { Component } from '@angular/core';
    import { Router, RouteConfigLoadStart, RouteConfigLoadEnd, RouterEvent } from '@angular/router';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      loading: boolean;
    
      constructor(router: Router) {
        this.loading = false;
    
        router.events.subscribe(
          (event: RouterEvent): void => {
            if (event instanceof RouteConfigLoadStart) {
              this.loading = true;
            } else if (event instanceof RouteConfigLoadEnd) {
              this.loading = false;
            }
          }
        );
      }
    }
    

      

  • 相关阅读:
    Arrays类
    异常
    Java日志第53天 2020.8.29
    Java日志第52天 2020.8.28
    Java日志第55天 2020.8.31
    Java日志第54天 2020.8.30
    测试:ATM
    dear mother
    Spring
    People like me
  • 原文地址:https://www.cnblogs.com/Answer1215/p/11420056.html
Copyright © 2020-2023  润新知