Angular 9-导航开始时获取目标网址

问题描述

我正在使用 Angular 9 。我需要知道导航开始后便是目标URL。 我想做这样的事情:

constructor(router: Router) {
    this.router = router;
}

this.router.events.pipe(
    filter(event => event instanceOf NavigationStart),map(event => {
        //get somehow the destination url of this navigation just started
    })
).subscribe()

是否只有通过使用Angular Router 才能实现这一目标?

解决方法

您可以使用

this.router.events.pipe(
  filter((e: Event): e is NavigationStart => e instanceof NavigationStart)
).subscribe((e: NavigationStart) => {
    e.url // contains the url you will go to
});