首先據我查的資料,this.$router.push路由跳轉有以下幾種形式:
1. 不帶參數
this.$router.push('/home')
this.$router.push({name:'home'})
this.$router.push({path:'/home'})
2. query傳參
this.$router.push({name:'home',query: {id:'1'}})
this.$router.push({path:'/home',query: {id:'1'}})
// html 取參 $route.query.id
// script 取參 this.$route.query.id
3. params傳參
this.$router.push({name:'home',params: {id:'1'}}) // 只能用 name
// 路由配置 path: "/home/:id" 或者 path: "/home:id" ,
// 不配置path ,第一次可請求,刷新頁面id會消失
// 配置path,刷新頁面id會保留
// html 取參 $route.params.id
// script 取參 this.$route.params.id
4. query和params區別
query類似 get, 跳轉之後頁面 url後面會拼接參數,類似?id=1, 非重要性的可以這樣傳, 密碼之類還是用params刷新頁面id還在
params類似 post, 跳轉之後頁面 url後面不會拼接參數 , 但是刷新頁面id 會消失
但是我使用this.$router.push({path:'/home',query: {id:'1'}})
和this.$router.push({name:'home',params: {id:'1'}})
調試時,都顯示query和params未定義。
最後成功的是this.$router.push({name:'itemdetail',query: {id:'1'}})
,也不知道爲什麼。
參考鏈接:vue 路由跳轉四種方式 (帶參數)