Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current name of route in Vue?

I want to get the name of the current route of vue-router, i have a component menu with navigation to another componentes, so i want to dispaly the name of the current route. I have this:

created(){     this.currentRoute;     //this.nombreRuta = this.$route.name; }, computed:{     currentRoute:{         get(){             this.nombreRuta = this.$route.name;         }     } } 

But the label of the name of the route does not change, the label only show the name of the first loaded route. Thank You

EDIT:

enter image description here

Image to show what i want

like image 998
Isaías Orozco Toledo Avatar asked Nov 02 '18 22:11

Isaías Orozco Toledo


People also ask

What is router View />?

Router-view It's basically the view where the components are rendered. It's like the main div that contains all the components, and it returns the component that match the current route.

What is router link in Vue?

router-link This allows Vue Router to change the URL without reloading the page, handle URL generation as well as its encoding.

What is meta in route Vue?

Sometimes, you might want to attach arbitrary information to routes like transition names, who can access the route, etc. This can be achieved through the meta property which accepts an object of properties and can be accessed on the route location and navigation guards.


1 Answers

You are using computed incorrectly. You should return the property in the function. See the docs for more information.

Here is your adapted example:

computed: {     currentRouteName() {         return this.$route.name;     } } 

You can then use it like this:

<div>{{ currentRouteName }}</div> 

You can also use it directly in the template without using a computed property, like this:

<div>{{ $route.name }}</div> 
like image 95
ssc-hrep3 Avatar answered Sep 24 '22 07:09

ssc-hrep3