如何在 vue.js 中获取当前路由的名称?

Ale*_*nor 0 html templates if-statement vue.js vuetify.js

我想在模板的语句route's中使用当前名称。v-if我读到有一种复杂的第三方方式,但我想要更好的东西。

<template v-if="$this.routes.getName() == '/customers'"> 
Run Code Online (Sandbox Code Playgroud)

Sat*_*hak 5

如果你用一个名字来注册路由那就太简单了

你的路线

{
 name: 'Foo',
 path: '/foo'
 component: 'Foo.vue'
}
Run Code Online (Sandbox Code Playgroud)

那么就只是

this.$route.name
Run Code Online (Sandbox Code Playgroud)

并且它会返回Foo

您可以与名称进行比较,但是如果您想与路径进行比较,那么

this.$route.path // it will return exact path after domain i.e. www.google.com/foo
if(this.$route.path === '/foo') {
  console.log('I am on foo route')
}
Run Code Online (Sandbox Code Playgroud)

尝试登录您的路线对象并探索您还有什么

console.log(this.$route)
Run Code Online (Sandbox Code Playgroud)