gpt4 book ai didi

html - 在 Angular 2 中的路线之间导航时显示加载屏幕

转载 作者:技术小花猫 更新时间:2023-10-29 11:27:37 26 4
gpt4 key购买 nike

在 Angular 2 中更改路线时如何显示加载屏幕?

最佳答案

当前的 Angular Router 提供了 Navigation Events。您可以订阅这些并相应地更改 UI。请记住计算其他事件,例如 NavigationCancelNavigationError 以在路由器转换失败时停止微调器。

app.component.ts - 你的根组件

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

@Component({})
export class AppComponent {

// Sets initial value to true to show loading spinner on first load
loading = true

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

// Shows and hides the loading spinner during RouterEvent changes
navigationInterceptor(event: RouterEvent): void {
if (event instanceof NavigationStart) {
this.loading = true
}
if (event instanceof NavigationEnd) {
this.loading = false
}

// 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
}
if (event instanceof NavigationError) {
this.loading = false
}
}
}

app.component.html - 您的 Root View

<div class="loading-overlay" *ngIf="loading">
<!-- show something fancy here, here with Angular 2 Material's loading bar or circle -->
<md-progress-bar mode="indeterminate"></md-progress-bar>
</div>

Performance Improved Answer:如果您关心性能,有更好的方法,实现起来稍微繁琐一些,但性能改进值得付出额外的努力。我们可以利用 Angular 的 NgZoneRenderer 打开/关闭将绕过 Angular 的旋转器,而不是使用 *ngIf 有条件地显示微调器当我们改变微调器的状态时改变检测。我发现与使用 *ngIfasync 管道相比,这可以使动画更流畅。

这与我之前的回答类似,只是做了一些调整:

app.component.ts - 你的根组件

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


@Component({})
export class AppComponent {

// Instead of holding a boolean value for whether the spinner
// should show or not, we store a reference to the spinner element,
// see template snippet below this script
@ViewChild('spinnerElement')
spinnerElement: ElementRef

constructor(private router: Router,
private ngZone: NgZone,
private renderer: Renderer) {
router.events.subscribe(this._navigationInterceptor)
}

// Shows and hides the loading spinner during RouterEvent changes
private _navigationInterceptor(event: RouterEvent): void {
if (event instanceof NavigationStart) {
// We wanna run this function outside of Angular's zone to
// bypass change detection
this.ngZone.runOutsideAngular(() => {
// For simplicity we are going to turn opacity on / off
// you could add/remove a class for more advanced styling
// and enter/leave animation of the spinner
this.renderer.setElementStyle(
this.spinnerElement.nativeElement,
'opacity',
'1'
)
})
}
if (event instanceof NavigationEnd) {
this._hideSpinner()
}
// 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._hideSpinner()
}
if (event instanceof NavigationError) {
this._hideSpinner()
}
}

private _hideSpinner(): void {
// We wanna run this function outside of Angular's zone to
// bypass change detection,
this.ngZone.runOutsideAngular(() => {
// For simplicity we are going to turn opacity on / off
// you could add/remove a class for more advanced styling
// and enter/leave animation of the spinner
this.renderer.setElementStyle(
this.spinnerElement.nativeElement,
'opacity',
'0'
)
})
}
}

app.component.html - 您的 Root View

<div class="loading-overlay" #spinnerElement style="opacity: 0;">
<!-- md-spinner is short for <md-progress-circle mode="indeterminate"></md-progress-circle> -->
<md-spinner></md-spinner>
</div>

关于html - 在 Angular 2 中的路线之间导航时显示加载屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37069609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com