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

Posted answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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;
        
      
    );
  

  

以上是关于[Angular] Show a Loading Indicator for Lazy Routes in Angular的主要内容,如果未能解决你的问题,请参考以下文章

angular6 页面加载数据时的loading提示

angular $http 请求数据的时候加载loading

v-loading指令

Angular 2 通过 [class.className] 绑定添加多个类

网页Loading效果

Angular 4 Lazy loading with named router-outlet 不起作用