vue跳轉(zhuǎn)頁(yè)面常用的幾種方法匯總
目錄
- 1:router-link跳轉(zhuǎn)
- 1.不帶參數(shù)
- 2.帶params參數(shù)
- 3.帶query參數(shù)
- 2:this.$router.push()
- 1.不帶參數(shù)
- 2.query傳參
- 3.params傳參
- 4.query和params區(qū)別
- 3:this.$router.replace()
- 4:this.$router.go(n)
- ps : 區(qū)別
- 總結(jié)
vue跳轉(zhuǎn)頁(yè)面有好幾種不同方法,下面將通過(guò)實(shí)例代碼給大家介紹,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下。
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ù)。
總結(jié)
以上所述就是給大家介紹的vue路由跳轉(zhuǎn)四種方式,僅為個(gè)人經(jīng)驗(yàn)。希望能給大家一個(gè)參考,也希望大家多多支持。
相關(guān)文章:
