vue-router傳參數(get和post方式)

本文主要介紹vue-router傳參數的兩種方式:
一、get方式
頁面跳轉
this.$router.push({path:'/xxx',query:{id:1}});//相似get傳參,經過URL傳遞參數
新頁面接收參數
this.$route.query.id
 
二、post方式
頁面跳轉
 //因爲動態路由也是傳遞params的,因此在 this.$router.push() 方法中 path不能和params一塊兒使用,不然params將無效。
須要用name來指定頁面。
 this.$router.push({name:'page2',params:{id:1}});//相似post傳參
 
新頁面接收參數
this.$route.params.id
 
三、注意:在頁面進行刷新的時候常常會遇到參數變了,可是新頁面接受的參數沒有變化。這種問題能夠經過加watch解決。
watch: {
      '$route'(to, from){ //在這裏從新刷新一下 this.getParams(); } }
相關文章
相關標籤/搜索