vue跳轉(zhuǎn)頁面常用的4種方法與區(qū)別小結(jié)
vue跳轉(zhuǎn)頁面有好幾種不同方法,下面將通過實(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中鏈接如果是'/'開始就是從根路由開始;如果不帶'/',則從當(dāng)前路由開始。 2.帶params參數(shù) <router-link :to="{name:'home', params: {id:123456}}"> // params傳參數(shù) (類似post) // 路由配置 path: "/home/:id" 或者 path: "/home:id" // 不配置path ,第一次可請(qǐng)求,刷新頁面id會(huì)消失;配置path,刷新頁面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)求,刷新頁面id會(huì)消失 // 配置path,刷新頁面id會(huì)保留 // html 取參 $route.params.id script 取參 this.$route.params.id 4. query和params區(qū)別 query類似get, 跳轉(zhuǎn)之后頁面url后面會(huì)拼接參數(shù),類似?id=123456, 非重要性的可以這樣傳, 密碼之類還是用params刷新頁面id還在 params類似post, 跳轉(zhuǎn)之后頁面url后面不會(huì)拼接參數(shù), 但是刷新頁面id會(huì)消失。
3. this.$router.replace()
用法同上,和第2個(gè)的this.$router.push方法一樣。
4. this.$router.go(n)
<button @click="upPage">[上一頁]</button> <button @click="downPage">[下一頁]</button> upPage() { this.$router.go(-1); // 后退一步記錄,等同于 history.back() }, downPage() { this.$router.go(1); // 在瀏覽器記錄中前進(jìn)一步,等同于 history.forward() }
向前或者向后跳轉(zhuǎn)n個(gè)頁面,n可為正整數(shù)或負(fù)整數(shù)
ps : 區(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)前頁面)。
this.$router.go(n)
向前或者向后跳轉(zhuǎn)n個(gè)頁面,n可為正整數(shù)或負(fù)整數(shù)。
總結(jié)
到此這篇關(guān)于vue跳轉(zhuǎn)頁面常用的4種方法與區(qū)別的文章就介紹到這了,更多相關(guān)vue跳轉(zhuǎn)頁面方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Vue中是如何實(shí)現(xiàn)cache緩存的
這篇文章分享一個(gè)比較有意思的東西,那就是Vue中如何實(shí)現(xiàn)cache緩存的,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-07-07詳解vue-cli 腳手架項(xiàng)目-package.json
本篇文章主要介紹了詳解vue-cli 腳手架項(xiàng)目-package.json,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07手把手教你Vue3實(shí)現(xiàn)路由跳轉(zhuǎn)
Vue Router是Vue.js的官方路由器,它與Vue.js核心深度集成,使使用Vue.js構(gòu)建單頁應(yīng)用程序變得輕而易舉,下面這篇文章主要給大家介紹了關(guān)于Vue3實(shí)現(xiàn)路由跳轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下2022-08-08