As you create more and more routes, the paths become difficult to manage and concatenate the parts together as just a giant string. Using named routes allows you to reference the route by a simple string then pass in the params as an object. This lesson covers configuring named routes in Nuxt.js in the nuxt.config.js
and the passing the named route to a router-link
.
[00:00] With routes, it's often way more convenient to have a named route, instead of worrying about the path, and then mapping to the path in a router link like this. The way you set up a name for your route is pretty self explanatory. You give it a property of name. Going to name this "user". Now, I have a named route.
[00:17] In my index, instead of putting together a URL to navigate to, I can delete all of this, and instead evaluate an object that has a name. I'm telling it to grab my "user" route based on that name. It has params, and this is another object with an ID of person.id.
[00:42] Now, this has become much more flexible if you need to move things around inside of your params, or if you're changing around URLs and you just want to keep the names intact inside of your router links, because now, everything just works the exact same, so refresh.
[00:57] Click on Palpatine, and we still have Palpatine, and all the other users work, as well.