在Angular中获取当前路径名称的最简单方法是什么? [英] What is the easiest way to get Current Route Path Name in Angular?

查看:71
本文介绍了在Angular中获取当前路径名称的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种获取当前路线路径名称的好方法.这是我最容易找到的.

I was looking for a good way to get the current route's path name. This was the easiest I could find.

this.route.snapshot.firstChild.url[0].path

有更好的方法吗?谢谢!

Is there a better way? Thanks!

推荐答案

感谢大家的回答.这是我发现我必须要做的.

Thanks everyone for the answers. Here is what I found that I had to do.

router.events.subscribe((event: Event) => {
  console.log(event);
  if (event instanceof NavigationEnd ) {
    this.currentUrl = event.url;
  }
});

这篇关于在Angular中获取当前路径名称的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆