Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Vue-router, is there any advantage of using named routes than just using the path directly?

According to official docs

Sometimes it is more convenient to identify a route with a name, especially when linking to a route or performing navigations.

So, how is this can be more convenient?

<router-link :to="{ name: 'user', params: { id: user.id }}">
   {{ user.name }}
</router-link>

vs

<router-link :to="'/user/' + user.id">
   {{ User.name }}
</router-link>

I feel like I'm missing something obvious.

like image 849
Christhofer Natalius Avatar asked Sep 08 '18 02:09

Christhofer Natalius


People also ask

What is the purpose of Vue router?

Vue Router is the official router for Vue. js. It deeply integrates with Vue. js core to make building Single Page Applications with Vue.

Is Vue router necessary?

You need a router when you need to sync URLs to views in your app. It's a very common need, and all the major modern frameworks now allow you to manage routing. The Vue Router library is the way to go for Vue. js applications.

Which of the following methods of Vue router can be invoked to return to the previous page?

To navigate to a different URL, use router.push . This method pushes a new entry into the history stack, so when the user clicks the browser back button they will be taken to the previous URL.

Can we define multiple router outlets in a component Vue?

Instead of having one single outlet in your view, you can have multiple and give each of them a name. A router-view without a name will be given default as its name. A working demo of this example can be found here.


1 Answers

I always use named routes.

The advantage is that you can change the path of the route without needing to change the path in every <router-link> or this.$router.push() call.

It's kind of like the reason why, in programming, we avoid magic values and instead use named constants – we can change the value in one place without needing to find-and-replace all occurrences of the value in our code.

Without using named routes, your code becomes tightly bound to the path of each route, you can't change one without needing to change the other. Named routes makes our code independent of the route paths – it'll work whatever the path of the route ends up being.

Another advantage is we can take advantage of param/query inheritance for nested routes – we don't need to reconstruct the full path, instead we can just pass the name of the child route and Vue will construct the full path with any existing params.

Suppose we have the following routes:

{
  name: 'user',
  path: '/user/:id',
  children: [
    {
      name: 'profile',
      path: 'profile'
    }
  ]
}

and the current route path is /user/1. To go to the profile page, we need to do either:

this.$router.push({ name: 'profile' })

or

this.$router.push('/user/' + this.$route.params.id + '/profile')

The former is simpler and less error prone.

like image 185
Decade Moon Avatar answered Oct 04 '22 04:10

Decade Moon