首页 新闻 会员 周边 捐助

angular 中如何判断是当前 url 的 reload 操作

0
悬赏园豆:30 [已解决问题] 解决于 2025-01-20 22:23

在 app.config.ts 中配置了 onSameUrlNavigation: 'reload',想在 Component 中判断如果是当前 url 的 reload 操作,更新列表,请问如何判断?

dudu的主页 dudu | 高人七级 | 园豆:25084
提问于:2025-01-20 15:14
< >
分享
最佳答案
0

通过 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));
    });
dudu | 高人七级 |园豆:25084 | 2025-01-20 22:21

还有一个解决方法

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;
            }
        });
    }
}
dudu | 园豆:25084 (高人七级) | 2025-05-19 20:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册