微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Angular 2旋转变压器带有加载指示器

我使用解析器将数据加载到组件中,但在数据未从服务器返回之前,组件不会加载.
我想知道在服务器响应之前是否有一种方法来呈现负载指示器.

解决方法

您可以使用“响应路由事件”策略来实现App以在发生任何路由事件时做出反应.要做到这一点,您需要在app.component.ts中使用以下代码

import { Component } from '@angular/core';
import { Router,Event,NavigationStart,NavigationEnd,NavigationError,NavigationCancel } from '@angular/router';

@Component({
  selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.sass']
})
export class AppComponent {

  loading: boolean = true;

  constructor(private router: Router) {
    router.events.subscribe((routerEvent: Event) => {
      this.checkRouterEvent(routerEvent);
    });
  }

  checkRouterEvent(routerEvent: Event): void {
    if (routerEvent instanceof NavigationStart) {
      this.loading = true;
    }

    if (routerEvent instanceof NavigationEnd ||
      routerEvent instanceof NavigationCancel ||
      routerEvent instanceof NavigationError) {
      this.loading = false;
    }
  }
}

在视图(html)中,例如:

<div id="loader" class="myloader" *ngIf="loading">
  Loading...
</div>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐