首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在我的整个数据被加载成角之前,我如何显示ngx旋转器?

在我的整个数据被加载成角之前,我如何显示ngx旋转器?
EN

Stack Overflow用户
提问于 2021-01-25 21:57:59
回答 1查看 2.1K关注 0票数 0

我有每一页的标题-图像和文字。有时图像加载缓慢,文本显示,在1-2秒后,heeader图像显示到它应该显示的地方,这是不好的用户体验。如何才能显示我的ngx旋转器,直到我的组件加载的整个数据作为路由组合起来?

我试过的- app.component.ts

代码语言:javascript
复制
import { Component } from '@angular/core';
import { NgxSpinnerService } from 'ngx-spinner';

import {
  Router,
  // import as RouterEvent to avoid confusion with the DOM Event
  Event as RouterEvent,
  NavigationStart,
  NavigationEnd,
  NavigationCancel,
  NavigationError
} from '@angular/router'

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

  loading = true;

  constructor(private spinner: NgxSpinnerService, private router: Router) {
    this.router.events.subscribe((e: RouterEvent) => {
      this.navigationInterceptor(e);
    })
  }

  ngOnInit() {

  }

  navigationInterceptor(event: RouterEvent): void {
    if (event instanceof NavigationStart) {
      this.loading = true;
      this.spinner.show();
      console.log(1111);
    }
    if (event instanceof NavigationEnd) {
      this.loading = false;
      this.spinner.hide();
    }
    // Set loading state to false in both of the below events to hide the spinner in case a request fails
    if (event instanceof NavigationCancel) {
      this.loading = false;
      this.spinner.hide();
    }
    if (event instanceof NavigationError) {
      this.loading = false;
      this.spinner.hide();
    }
  }


}

应用程序组件html

代码语言:javascript
复制
<ngx-spinner bdColor="rgba(51,51,51,0.8)" size="medium" color="#fff" type="ball-scale-multiple">
  <p style="font-size: 20px; color: white">Loading...</p>
</ngx-spinner>

但这是行不通的。当我的路线在吟唱的时候,当我的路线在那里呈现时,我没有得到旋转的声音。

EN

回答 1

Stack Overflow用户

发布于 2021-01-26 02:27:17

尝试使用生命周期挂钩。通过使用路由器事件,我们无法定义组件是否已完全加载。对于这种情况,您可以使用ngAfterViewInit生命周期。

检查以下站点作为参考

https://angular.io/guide/lifecycle-hooks

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65893005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档