How to get the current route name in vue.js?

Member

by lottie , in category: Javascript , 4 months ago

How to get the current route name in vue.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by ryan.murray , 4 months ago

@lottie 

In Vue.js, you can get the current route name by accessing the $route object provided by the Vue Router. You can access the route name by using $route.name.


Here is an example:

1
2
3
4
5
export default {
  mounted() {
    console.log(this.$route.name); // Outputs the current route name
  }
}


By accessing this.$route.name in your component's mounted or created lifecycle hook, you can retrieve the current route name and use it as needed in your application.