如何在 Angular 2 中進行路由時顯示載入畫面
在路由之間的轉換期間顯示載入畫面可透過提供視覺回饋來增強使用者體驗。 Angular 2 提供了管理此類轉換的內建支援。
實作載入畫面的關鍵是訂閱 Angular 的 RouterEvents。這些事件提供有關導航過程當前狀態的資訊。
改進了答案以增強性能
為了獲得更好的性能,請考慮避免 ngIf加載微調器的條件顯示。相反,利用 NgZone 和 Renderer 在 Angular 的更改偵測週期之外切換微調器的可見性。
@Component({}) export class AppComponent { @ViewChild('spinnerElement') spinnerElement: ElementRef constructor(private router: Router, private ngZone: NgZone, private renderer: Renderer) { router.events.subscribe(this._navigationInterceptor) } private _navigationInterceptor(event: RouterEvent): void { if (event instanceof NavigationStart) { // Show spinner outside Angular's zone to prevent change detection this.ngZone.runOutsideAngular(() => { this.renderer.setElementStyle( this.spinnerElement.nativeElement, 'opacity', '1' ) }) } if (event instanceof NavigationEnd) { this._hideSpinner() } // Hide spinner in case of failure if (event instanceof NavigationCancel || event instanceof NavigationError) { this._hideSpinner() } } private _hideSpinner(): void { // Hide spinner outside Angular's zone to prevent change detection this.ngZone.runOutsideAngular(() => { this.renderer.setElementStyle( this.spinnerElement.nativeElement, 'opacity', '0' ) }) } }
HTML
<div class="loading-overlay"><p>透過訂閱RouterEvents 並有條件地顯示在變化偵測之外載入微調器,您可以獲得更流暢、回應更靈敏的導航體驗。 </p></div>
以上是如何在 Angular 2 中的路由轉換期間實作載入畫面?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

重構自己的代碼看起來是什麼樣的?約翰·瑞亞(John Rhea)挑選了他寫的一個舊的CSS動畫,並介紹了優化它的思維過程。

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatoryand and powerincreatingsmoothcsssanimations.keytricksinclude:1)definingsmoothtransitionsbetnestates,2)使用AnimatingMultatingMultationMultationProperPertiessimultane,3)使用使用4)使用BombingeNtibalibility,4)使用CombanningWiThjavoFofofofoftofofo

CSSCOUNTERSAREDOMANAGEAUTOMANAMBERINGINWEBDESIGNS.1)他們可以使用forterablesofcontents,ListItems,and customnumbering.2)AdvancedsincludenestednumberingSystems.3)挑戰挑戰InclassINCludeBrowsEccerCerceribaliblesibility andperformiballibility andperformissises.4)創造性

使用滾動陰影,尤其是對於移動設備,是克里斯以前涵蓋的一個微妙的UX。傑夫(Geoff)涵蓋了一種使用動畫限制屬性的新方法。這是另一種方式。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

Dreamweaver Mac版
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。