vue的路由如何传递参数?第二个页面如何拿到参数

路由main.js

{
    name:'hotel',
    path:'/hotel/:id',
    component:Hotel
}
  1. list.vue页面 <router-link :to="{path:'/hotel/'+item.id}">是这样子的,我想把item.id传给hotel.vue页面,。点击url会跳转到http://localhost:8080/#/hotel/1

  2. 我在hotel.vue页面中

  mounted(){
      console.log(this.$vm.$route.params) //
      //我想在这里得到参数,会报下面这个错误
//$route is not defined
  }

得不到参数?怎么做

另外 如果改成下面<router-link :to="{path:'/hotel',params:{id:item.id}}">这样,http://localhost:8080/#/hotel不知道为什么连后缀都没了

阅读 50.4k
5 个回答

<router-link :to="{path:'/hotel',params:{id:item.id}}">

<router-link :to='{name:"hotel", params:{ id:item.id } }'>
这样试试下

打印this.route会报错$route is not defined,我需要也在hotel.vue中import一下 vue-router么,我实在main.js中引用

this.$route.params

新手上路,请多包涵

我用这个获取到的<router-link :to='{name:"hotel", params:{ id:item.id } }'>好像是因为params需要通过name来获取

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
1 篇内容引用
推荐问题