vue 路由和傳參

1.不帶參
  <router-link :to="{name:'home'}"> 
  <router-link :to="{path:'/home'}">

  this.$router.push('/home')
  this.$router.push({name:'home'})
  this.$router.push({path:'/home'})

  this.$router.replace('/home')
  this.$router.replace({name:'home'})
  this.$router.replace({path:'/home'})

2.帶參(params)-- params傳參數(shù) (類似post)
  //配置path,刷新頁面id會(huì)保留
  // 路由配置 path: "/home/:id" 或者 path: "/home:id" 
  <router-link :to="{name:'home', params: {id:1}}"> 

  this.$router.push({name:'home',params: {id:'1'}}) // 只能用 name

3.帶參(query)--query傳參數(shù) (類似get,url后面會(huì)顯示參數(shù))
  <router-link :to="{name:'home', query: {id:1}}"> 

  this.$router.push({name:'home',query: {id:'1'}})
  this.$router.push({path:'/home',query: {id:'1'}})

4. this.$router.replace() (用法同上,push)

5. this.$router.go(n)
   向前或者向后跳轉(zhuǎn)n個(gè)頁面,n可為正整數(shù)或負(fù)整數(shù)
1.取參
// html 取參 $route.params.id、$route.query.id
// script 取參 this.$route.params.id、 this.$route.query.id

2. query和params區(qū)別
query類似 get, 跳轉(zhuǎn)之后頁面 url后面會(huì)拼接參數(shù),類似?id=1, 刷新頁面id還在。非重要性的可以這樣傳, 密碼之類還是用params
 params類似 post, 跳轉(zhuǎn)之后頁面 url后面不會(huì)拼接參數(shù) , 但是刷新頁面id 會(huì)消失,配置path,刷新頁面id會(huì)保留

3.區(qū)別
this.$router.push
跳轉(zhuǎn)到指定url路徑,并想history棧中添加一個(gè)記錄,點(diǎn)擊后退會(huì)返回到上一個(gè)頁面

this.$router.replace
跳轉(zhuǎn)到指定url路徑,但是history棧中不會(huì)有記錄,點(diǎn)擊返回會(huì)跳轉(zhuǎn)到上上個(gè)頁面 (就是直接替換了當(dāng)前頁面)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容