Skip to content Skip to sidebar Skip to footer

How To Pass Props In Vue Router

Hi guys i been trying to pass props in vue routes so far i was able to achieve this below, but is not what i wanted. So far this works route.js { path: '/register/', name:

Solution 1:

I think you have some misconception about the usage of params in vue-router. Params should be part of the path.

for e.g., if the path setting is like

{
    path: '/register/:id',
    // ...
}

<router-link :to="{ name: 'register', params: { id: 'abc123', isRegisteringMe:'true' }}">Register</router-link> will link the user to register/abc123.

since the path setting is path: '/register/:id',, isRegisteringMe param is not defined and won't be displayed in the url.

Params should be part of the path. I see a few way you can change this.

  1. use url query for isRegisteringMe. for eg, /register?isRegisteringMe=true ( <router-link :to="{ name: 'register', query: { isRegisteringMe: 'true' }}"). and get the value from $route.query.isRegisteringMe

update:

if you want user to always have isRegisteringMe:true by default do a redirection in register component's mounted lifecycle event.

  1. set isRegisteringMe in the app's state, or preferably in vuex if you are using it. then in register component, get the state and use it accordingly.

hope that this move you towards the right direction.

Post a Comment for "How To Pass Props In Vue Router"