javascript - 使用 Angular 材质进度条加载页面
问题描述
我有一个 Angular 项目,我在其中广泛使用Angular Material library。我想使用进度条组件来显示页面何时加载或我何时进行 api 调用。例如,当我在等待 Stripe 的回复时。
启动进度条似乎很简单,只需在服务中使用全局变量来指示页面何时加载。我正在考虑为此使用路由器。
但是,我想显示页面加载的实际进度。例如,当您观看 youtube 视频时。组件 api 使用 value 属性来显示进度量。但是如何获取页面加载的进度呢?
我知道还有其他库(例如 ngx)使用它,但如果可能的话,我想使用 Angular Material 库。
任何想法如何实现这一目标?
解决方案
为时已晚,但也许其他人需要它:
有这方面的软件包,例如ng2-slim-loading-bar。
但是,如果您想使用 Material Progress Bar 手动执行此操作,请查看此示例。
它确实给人一种错误的进展错觉,因为它会随着时间的推移而增加,并且如果它在没有完成负载的情况下达到 95%,那么它会停止直到发生这种情况。我不知道是否有任何方法可以计算请求的真实进度,那将是完美的。
编辑:查看有关Tracking 和显示请求进度的 Angular 文档,这样您就可以实现一个相当真实的进度条。
零件:
import { Component } from '@angular/core';
import {
NavigationCancel,
Event,
NavigationEnd,
NavigationError,
NavigationStart,
Router,
} from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
export class AppComponent {
progressValue = 0;
progressColor = 'primary';
progressTimer: number;
// This will be used to force stop (if an error occurs, or the user stops loading)
stopProgress = false;
constructor(private router: Router) {
this.router.events.subscribe((event: Event) => {
this.navigationObserver(event);
});
}
private navigationObserver(event: Event): void {
if (event instanceof NavigationStart) {
// Increase 1% every 25 milliseconds, adjust it to your preference
this.progressTimer = setInterval(() => {
this.loading();
}, 25);
}
if (event instanceof NavigationEnd) {
// When the navigation finishes, fill the bar completely
this.progressValue = 100;
/*
* Uncomment this block to simulate a delay (for testing), because if you
* are in a local environment or the request is to a 'light' or very fast resource,
* the progress bar will appear at 100%.
*/
/*
setTimeout(() => {
this.progressValue = 100;
}, 2000);
*/
}
/*
* If the navigation is canceled or an error occurs,
* stop the progress bar and change its color.
*/
if (event instanceof NavigationCancel) {
this.stopProgress = true;
this.progressColor = 'accent';
}
if (event instanceof NavigationError) {
this.stopProgress = true;
this.progressColor = 'warn';
}
}
// Function to increase the value of the progress bar
private loading(): void {
/*
* Leave 5% in case an unusual delay occurs, in the previous
* function it is filled to 100% if the load ends successfully
*/
if (this.progressValue >= 95 || this.stopProgress) {
clearInterval(this.progressTimer);
} else {
this.progressValue++;
}
}
}
模板:
<mat-progress-bar [value]="progressValue" [color]="progressColor">
</mat-progress-bar>
<div *ngIf="progressValue == 100; else elseBlock">
<h1>Loaded!</h1>
</div>
<ng-template #elseBlock>
<h1>Loading...</h1>
</ng-template>
推荐阅读
- java - OSPermissionSubscriptionState:无法解析符号
- python - 当我尝试删除所有不以特定名称开头的行时,如何将列名保留在数据框中?
- python-3.x - 将操作应用于 numpy 数组中的特定列
- jackson - 字符串的杰克逊反序列化为 LocalDateTime 不一致
- javascript - 如何使用 t-jquery 和 t-operation 扩展模板属性(Odoo 14)
- google-bigquery - Concatenate strings in BigQuery
- sql - Netsuite 保存的搜索中的 TRIM 或 REPLACE
- webpack - 当我在观看模式下保存文件时,Storybook webpack 构建冻结在 70%
- python - 我可以在 Dash 中缩小旭日形图与其色标之间的间距吗?
- javascript - 无法使用 requests_html 抓取网站