router-link to params

时间:2018-05-14 17:18:47

标签: javascript vuejs2 vue-router

我尝试使用 vue-router 转到vue app的UserDetail路径。

我的路线是:

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

和路由器链接:

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

但这显然不是正确的语法。我通常使用命名路由(例如参见https://router.vuejs.org/en/api/router-link.html)这类东西,但我怎么能使用路径?

编辑#1

将鼠标悬停在其中一个链接上:

enter image description here

1 个答案:

答案 0 :(得分:0)

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.