溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

vue跳轉(zhuǎn)頁(yè)面常用的方法有哪些

發(fā)布時(shí)間:2022-03-11 16:23:49 來(lái)源:億速云 閱讀:164 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹“vue跳轉(zhuǎn)頁(yè)面常用的方法有哪些”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“vue跳轉(zhuǎn)頁(yè)面常用的方法有哪些”文章能幫助大家解決問(wèn)題。

1:router-link跳轉(zhuǎn)

1.不帶參數(shù)
<router-link :to="{name:'home'}"> 
<router-link :to="{path:'/home'}"> //name,path都行, 建議用name 
// 注意:router-link中鏈接如果是'/'開(kāi)始就是從根路由開(kāi)始;如果不帶'/',則從當(dāng)前路由開(kāi)始。
 
2.帶params參數(shù)
<router-link :to="{name:'home', params: {id:123456}}"> 
// params傳參數(shù) (類似post)
// 路由配置 path: "/home/:id" 或者 path: "/home:id" 
// 不配置path ,第一次可請(qǐng)求,刷新頁(yè)面id會(huì)消失;配置path,刷新頁(yè)面id會(huì)保留。
// html 取參 $route.params.id    script 取參 this.$route.params.id
 
3.帶query參數(shù)
<router-link :to="{name:'home', query: {id:123456}}"> 
// query傳參數(shù) (類似get,url后面會(huì)顯示參數(shù))
// 路由可不配置
// html 取參 $route.query.id    script 取參 this.$route.query.id

2:this.$router.push()

1. 不帶參數(shù)
this.$router.push('/home')
this.$router.push({name:'home'})
this.$router.push({path:'/home'})
 
2. query傳參 
this.$router.push({name:'home',query: {id:'123456'}})
this.$router.push({path:'/home',query: {id:'123456'}})
// html 取參 $route.query.id    script 取參 this.$route.query.id
 
3. params傳參
this.$router.push({name:'home',params: {id:'123456'}}) // 只能用 name
// 路由配置 path: "/home/:id" 或者 path: "/home:id" ,
// 不配置path ,第一次可請(qǐng)求,刷新頁(yè)面id會(huì)消失
// 配置path,刷新頁(yè)面id會(huì)保留
// html 取參 $route.params.id    script 取參 this.$route.params.id
 
4. query和params區(qū)別
query類似get, 跳轉(zhuǎn)之后頁(yè)面url后面會(huì)拼接參數(shù),類似?id=123456, 非重要性的可以這樣傳, 密碼之類還是用params刷新頁(yè)面id還在
params類似post, 跳轉(zhuǎn)之后頁(yè)面url后面不會(huì)拼接參數(shù), 但是刷新頁(yè)面id會(huì)消失。

3.  this.$router.replace() 

用法同上,和第2個(gè)的this.$router.push方法一樣。

4.  this.$router.go(n) 

<button @click="upPage">[上一頁(yè)]</button>
<button @click="downPage">[下一頁(yè)]</button>
upPage() {
this.$router.go(-1);  // 后退一步記錄,等同于 history.back()
},
downPage() {
this.$router.go(1);   // 在瀏覽器記錄中前進(jìn)一步,等同于 history.forward()
}

向前或者向后跳轉(zhuǎn)n個(gè)頁(yè)面,n可為正整數(shù)或負(fù)整數(shù)

ps : 區(qū)別

this.$router.push

跳轉(zhuǎn)到指定url路徑,并向history棧中添加一個(gè)記錄,點(diǎn)擊后退會(huì)返回到上一個(gè)頁(yè)面。

this.$router.replace

跳轉(zhuǎn)到指定url路徑,但是history棧中不會(huì)有記錄,點(diǎn)擊返回會(huì)跳轉(zhuǎn)到上個(gè)頁(yè)面 (直接替換當(dāng)前頁(yè)面)。

this.$router.go(n)

向前或者向后跳轉(zhuǎn)n個(gè)頁(yè)面,n可為正整數(shù)或負(fù)整數(shù)。

關(guān)于“vue跳轉(zhuǎn)頁(yè)面常用的方法有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI