我正在使用Angular 4,並且我希望在路由導航之間顯示加載屏幕(基本微調)。 我已經使用this技術來訂閱正確的事件,並使用style.visibility爲了顯示/隱藏我的微調。Angular 4正確顯示加載屏幕
出於某種原因,雖然我正確地檢測到導航的開始和結束事件,但我的微調不會顯示。實際上,儘管能見度字段是正確更改,但微調器仍不會顯示。
我設法顯示它的唯一方法是迫使它在任何時候都被設置爲'可見'而不是'隱藏'。
附上我的代碼的相關章節:
app.component.ts:
constructor (private utils: UtilsService, private router: Router) {
\t \t // Intercept all navigation events
\t \t router.events.subscribe((event: RouterEvent) => {
\t \t \t this.navigationInterceptor(event)
\t \t })
\t \t // Calls our initializer
\t \t utils.init();
\t }
\t /* Intercept the navigator in order to display a loading screen */
\t navigationInterceptor(event: RouterEvent): void {
\t \t // Starting the navigation
\t \t if (event instanceof NavigationStart) {
\t \t \t this.loading = true;
\t \t }
\t \t // The navigation ended
\t \t if (event instanceof NavigationEnd) {
\t \t \t this.loading = false;
\t \t }
\t \t // Set loading state to false in both of the below events to hide the spinner in case a request fails
\t \t if (event instanceof NavigationCancel) {
\t \t \t this.loading = false;
\t \t }
\t \t if (event instanceof NavigationError) {
\t \t \t this.loading = false;
\t \t }
\t \t // Get our loading container
\t \t var loading_container = document.getElementById('loader-container');
\t \t // If our loading container isn't available, this is the application first launch (ie. full refresh)
\t \t // so we need to display a full loading logo
\t \t if (loading_container) {
\t \t \t // This is just a redirect, not a full refresh, so just hide or show the loading screen
\t \t \t document.getElementById('loader-container').style.visibility = this.loading ? 'visible' : 'hidden';
\t \t }
\t }
app.component.html:
<div style="position: fixed; left: 220px; width: calc(100% - 220px); height: 100%; z-index: 10000; background-color: rgba(255, 255, 255, 0.6);" id = "loader-container">
\t <div style = "top: 30%;" class="sk-spinner sk-spinner-double-bounce">
\t \t <div class="sk-double-bounce1"></div>
\t \t <div class="sk-double-bounce2"></div>
\t </div>
</div>
<!-- Main view/routes wrapper-->
<router-outlet>
</router-outlet>
謝謝Rahul。我用* ngIf但它仍然不起作用。我在NavigationEnd條件中添加了「setTimeout」,現在只有在導航完成後,微調器纔會顯示短時間**。我試圖在當前頁面顯示微調,而另一頁正在加載。 有關爲什麼會發生這種情況的任何想法? –
@WalterWhite然後你可能需要添加路由器解析到它也當你想顯示組件完全準備好時,鏈接使用路由器解析也檢查。它的顯示意味着它僅適用於沒有組件擁有數據的路由器 –