vue-router 可以根据 URL 来获取 routers 定义的 name 属性吗?

我的 Vue router 配置是这样的

routes: [
    {
      path: '/',
      name: 'Index',
      component: Index,
    },
    {
      path: '/hello',
      name: 'Hello',
      component: Hello,
    },
    {
      path: '/about',
      name: 'About',
      component: About,
    },
]

假如我访问的地址是 http://localhost/#/hello

我能获取到与路径 /hello 对应的 name 值 Hello 吗?

阅读 9.3k
2 个回答

this.$route.name

可以的。this.$route.name 就可以获取到对应的name了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题