简体   繁体   中英

router-link to path with params

I'm trying to go to a UserDetail path in my vue app using vue-router .

My routes are :

const routes = [
  { path: '/users', component: UserList },
  { path: '/users/:user_id', component: UserDetail },
  { path: '/bar', component: Bar }
]

And router link :

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

But this is clearly not the correct syntax. I normally used named routes (see https://router.vuejs.org/en/api/router-link.html for example) for this type of stuff but how could I use path?

edit #1

mousing over one of the links:

在此处输入图片说明

The solution is simple. Just do like router-link docs says:

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

If you want to pass the parameters like this, you should use name instead of path inside the :to .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM