首页 新闻 会员 周边 捐助

在 angular Router 的 NavigationStart 如何获取 url path

0
悬赏园豆:30 [已解决问题] 解决于 2025-06-19 17:56

通过 RouterEvent 的 url 拿到的值不仅包含 path,还包含查询参数

async ngOnInit() {
    this._router.events.subscribe(async (ev) => {
        if (ev instanceof NavigationStart) {
            debounceTime(50);
            if (ev.url !== this._previousUrl) {
                await this.setActiveTab(ev.url);
            }
            this._previousUrl = ev.url;
        } 
    });
}
dudu的主页 dudu | 高人七级 | 园豆:24691
提问于:2025-06-19 17:47
< >
分享
最佳答案
0

提了一个错误的问题,在 NavigationStart 的时候,ActivatedRoute 可能还没被更新

通知 deepseek 的思考过程知道的

  • NavigationStart is an event from the Router service, which happens at the beginning of a navigation.
  • At the moment of NavigationStart, the ActivatedRoute might not yet be updated because the navigation hasn't completed.
dudu | 高人七级 |园豆:24691 | 2025-06-19 17:55

自己处理一下 url

const path = ev.url.split('?')[0];
dudu | 园豆:24691 (高人七级) | 2025-06-19 18:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册