【问题标题】:Show loading animation while retrieving data (Angular 10)在检索数据时显示加载动画(Angular 10)
【发布时间】:2020-09-05 03:09:27
【问题描述】:

我在检索数据时尝试在 Angular 10 中显示加载动画。如果我使用特定的时间值,我可以显示加载动画,但我无法在收集数据时显示它。请在下面查看我的代码的 sn-p。而不是手动调用加载动画,我希望它只在“getAllDocuments”函数运行时运行。有时我的硬编码时间值太短而其他时间值太长。我希望它每次都是正确的。


ngOnInit() {
    this.getAllDocuments();
    this.triggerLoadingAnimation(12);
  }

  ngAfterViewInit(): void {
    this.dataSource.sort = this.sort;
    this.dataSource.paginator = this.paginator;
  }

  public triggerLoadingAnimation(num){
    this.spinner.show();

    setTimeout(() => {
      this.spinner.hide();
    }, num*1500);
  }

  async getAllDocuments() {
    this.repoService.getData('/api/Prometheus/Documents')
    .subscribe(res => {
      this.dataSource.data = res as Document[];
    })
  }

【问题讨论】:

  • 看看这个npmjs.com/package/ng-http-loader。这将在后端进行全局 http 调用。无需手动隐藏和显示加载器。
  • 在发送请求之前启动微调器并完成它的 finalize() 请求方法

标签: javascript angular loading angular10


【解决方案1】:

你很亲密。
我还添加了错误处理,删除了不必要的异步,并在构造函数而不是 ngOnInit 中运行 ajax 调用以避免不必要的等待时间。

constructor(private repoService: RepoService) {
    this.getAllDocuments();
}

ngOnInit() {
  // stuff that really need @Input data to be filled
}

ngAfterViewInit(): void {
    this.dataSource.sort = this.sort;
    this.dataSource.paginator = this.paginator;
}

getAllDocuments() {
    this.spinner.show();
    this.repoService.getData('/api/Prometheus/Documents')
    .subscribe((res: Document[]) => {
      this.dataSource.data = res;
      this.spinner.hide();
    },
    error => {
      this.spinner.hide();
      console.error('error while retrieving documents', error);
      // handle error
    })
}

【讨论】:

  • 错误方法不足以解决任何错误,您需要使用 finalize 方法隐藏微调器
  • 这需要微调非常感谢!
  • @pc_coder 我不这么认为。 finalize 在可观察完成时触发。对于 HTTP 调用,它总是会触发错误或成功。您能否明确说明 observable 将在不触发成功或错误处理程序的情况下完成的情况?
  • @Random,你是对的,但在这种情况下你已经做了两次代码,成功和错误,finalize 就是为了这些目的
  • @MandeepSingh 我认为错误不会触发 finalize 回调。没有办法像 try/catch/finally 一样使用 Observables。
【解决方案2】:

您做错了,您应该调用微调器以显示何时进行 API 调用并在检索数据时将其隐藏。

更好的方法是在构造函数中调用它,因为在完成创建组件 DOM 时调用 ngOnInit,通过构造函数注入所有必需的依赖项并处理输入绑定

constructor() {
    this.getAllDocuments();
}


ngOnInit() {
    //this.getAllDocuments();
  }

  ngAfterViewInit(): void {
    this.dataSource.sort = this.sort;
    this.dataSource.paginator = this.paginator;
  }

  async getAllDocuments() {
    this.spinner.show();
    this.repoService.getData('/api/Prometheus/Documents')
    .subscribe((res: Document[]) => {
      this.dataSource.data = res;
    }, (err) => {
        //handle the errors
         console.log(err);
    }, () => {
        this.spinner.hide();
    });
  }

【讨论】:

  • 错误方法不足以隐藏任何错误在 finalize() 方法中使用一次
  • 对其进行了编辑,在 finalize 中处理了微调器动画的隐藏,而不是成功和错误
【解决方案3】:

加载微调器是一种很好的用户体验技术,但是您需要加载多少微调器才能让用户满意?可能取决于应用程序的大小。 由于您需要在检索数据时显示微调器,因此使用全局微调器将是一种更好的方法。使用全局微调器,只要检索数据,微调器就会显示。 下面是一个很好的拦截器实现来显示一个微调器 Using Interceptor to show spinner

【讨论】:

    【解决方案4】:

    您可以使用以下代码:

    getAllDocuments() {
     this.spinner.show(); // SHOW WHEN DATA IS LOADING
        this.repoService.getData('/api/Prometheus/Documents')
        .subscribe(res => {
          this.dataSource.data = res as Document[];
         this.spinner.hide();  //  HIDE WHEN DATA IS LOADED
        })
      }
    

    【讨论】:

    • 这个逻辑是错误的,在错误的情况下你不要在这里隐藏微调器!你需要隐藏在finalize中
    • @pc_coder 问题是在加载数据时显示微调器。我已经给出了答案。
    • @pc_coder 至于其他给定的解决方案,甚至其他解决方案也没有处理其他可能的情况,即空响应、服务器端错误代码、服务器故障。
    【解决方案5】:

    您可以在同一个函数中显示和隐藏微调器,而不是为此创建新函数。

    async getAllDocuments() {
        this.spinner.show();
        this.repoService.getData('/api/Prometheus/Documents')
        .subscribe(res => {
              this.dataSource.data = res as Document[];
              this.spinner.hide();
        });
        
    }
    

    【讨论】:

    • 是的,需要放在里面。更新了答案。
    • 这个逻辑是错误的,在错误的情况下你不要在这里隐藏微调器!你需要隐藏在finalize中
    • @pc_coder 你能举个例子来说明你会怎么做吗?
    猜你喜欢
    • 2015-04-30
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    相关资源
    最近更新 更多