当前,我创建了一个微调器组件
import { Router, NavigationStart, NavigationEnd, NavigationCancel, NavigationError } from '@angular/router';
import { Component, OnInit, OnDestroy, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'app-spinner',
templateUrl: './spinner.component.html',
styleUrls: ['./spinner.component.scss'],
encapsulation: ViewEncapsulation.None
})
export class SpinnerComponent implements OnDestroy {
public isSpinnerVisible = true;
constructor(private router: Router) {
this.router.events.subscribe(event => {
if (event instanceof NavigationStart) {
this.isSpinnerVisible = true;
} else if ( event instanceof NavigationEnd || event instanceof NavigationCancel || event instanceof NavigationError) {
this.isSpinnerVisible = false;
}
}, () => {
this.isSpinnerVisible = false;
});
}
ngOnDestroy(): void {
this.isSpinnerVisible = false;
}
}
内部app.component.html
<router-outlet>
<app-spinner></app-spinner>
</router-outlet>
spinner.component.html
<div class="preloader" *ngIf="isSpinnerVisible">
<div class="spinner">
<div class="double-bounce1"></div>
<div class="double-bounce2"></div>
</div>
</div>
但是,它没有显示,即使某些背景ajax尚未完成,NavigationEnd也会快速触发
答案 0 :(得分:1)
public isSpinnerVisible = true;
constructor(private router: Router) {
this.router.events.subscribe(event => {
if (event instanceof NavigationStart) {
this.isSpinnerVisible = true;
} else if ( event instanceof NavigationEnd || event instanceof NavigationCancel || event instanceof NavigationError) {
this.isSpinnerVisible = false;
}
}, () => {
this.isSpinnerVisible = false;
});
}
<router-outlet>
<app-spinner *ngIf="isSpinnerVisible=='ture'"></app-spinner>
</router-outlet>