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

dc_*_*a98 1 url-routing angular-routing angular angular-router angular9

我正在使用Angular 9。我需要在导航开始后立即知道哪个将是目标网址。我想做这样的事情:

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()
Run Code Online (Sandbox Code Playgroud)

有没有办法只使用 Angular Router来实现这一点?

Tim*_*age 5

您可以使用

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
});
Run Code Online (Sandbox Code Playgroud)