Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use named routes in Nuxt js?

Problem

How do I programmatically navigate as a user in Nuxt when routes are nested? I know that I can use this.$router.push({ name: "exchanges" }) to push a user to the exchanges pages, but when routes are nested, I am not sure what to do. If I use this.$router.push({ path: "exchanges/create" }) or this.$router.push("exchanges/create"), "exchanges/create" gets added to the URL even if I am already at that current URL.

Also, the page component names are "ExchangesPage", "EachExchangePage", "CreateExchangePage", and "HomePage". This didn't work when I tried to use it as the name for the named routes.

Current File Structure in ./pages

pages/
--| exchanges/
-----| _userId/
--------| index.vue
-----| create/
--------| index.vue
--| index.vue
like image 768
Alex Avatar asked Dec 19 '20 03:12

Alex


People also ask

What router does NUXT use?

Nuxt automatically generates the vue-router configuration for you, based on your provided Vue files inside the pages directory. That means you never have to write a router config again!

How do I get the current route name in NUXT?

Answer: Use the Vue. To get current route name from a URL in a Nuxt app that uses Vue Router you can use vue. js route objects like this. $route.name or this. $route.


1 Answers

for a quick check about your routes in a nuxt project you can do this:

in your project's directory navigate to this:

.nuxt/router.js

this is the file with all your routes generated by nuxt and you can check their exact name to navigate to them, look at this picture:

enter image description here

according to the image above which is from one of my projects I can do this:

this.$router.push({ name: 'universities-id' });
like image 163
hamid niakan Avatar answered Sep 30 '22 22:09

hamid niakan