通过 router.getCurrentNavigation
获取当前 url 与先前访问的 url 进行比较可以判断出来
this.router.events.pipe(
filter(x => x instanceof NavigationEnd),)
.subscribe(() => {
console.log('NavigationEnd');
const nav = this.router.getCurrentNavigation();
const currentUrl = nav?.initialUrl.toString();
const previousUrl = nav?.previousNavigation?.initialUrl.toString()
console.log('reload: ' + (currentUrl === previousUrl));
});
还有一个解决方法
export class AppLayoutTabsComponent implements OnInit, AfterViewInit {
private _previousUrl = '';
constructor(
private readonly router: Router,
private readonly spinner: SpinnerService) { }
async ngOnInit() {
this.router.events.subscribe(async (ev) => {
if (ev instanceof NavigationStart) {
debounceTime(50);
if (ev.url !== this._previousUrl) {
await this.spinner.toggle(true);
}
this._previousUrl = ev.url;
}
});
}
}