本文主要介紹vue-router傳參數的兩種方式:
1、get方式
頁面跳轉
this.$router.push({path:'/xxx',query:{id:1}});//類似get傳參,通過URL傳遞參數
新頁面接收參數
this.$route.query.id
2、post方式
頁面跳轉
//由於動態路由也是傳遞params的,所以在 this.$router.push() 方法中 path不能和params一起使用,否則params將無效。
需要用name來指定頁面。
this.$router.push({name:'page2',params:{id:1}});//類似post傳參
新頁面接收參數
this.$route.params.id
3、注意:在頁面進行刷新的時候經常會遇到參數變了,但是新頁面接受的參數沒有變化。這種問題可以通過加watch解決。
watch: {
'$route'(to, from){ //在這里重新刷新一下 this.getParams(); } }