vue跳轉(zhuǎn)頁面常用的幾種方法匯總
vue跳轉(zhuǎn)頁面有好幾種不同方法,下面將通過實例代碼給大家介紹,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下。
1:router-link跳轉(zhuǎn)
1.不帶參數(shù)
<router-link :to="{name:'home'}">? <router-link :to="{path:'/home'}"> //name,path都行, 建議用name? // 注意:router-link中鏈接如果是'/'開始就是從根路由開始;如果不帶'/',則從當前路由開始。?
2.帶params參數(shù)
<router-link :to="{name:'home', params: {id:123456}}">? // params傳參數(shù) (類似post) // 路由配置 path: "/home/:id" 或者 path: "/home:id"? // 不配置path ,第一次可請求,刷新頁面id會消失;配置path,刷新頁面id會保留。 // html 取參 $route.params.id ? ?script 取參 this.$route.params.id?
3.帶query參數(shù)
<router-link :to="{name:'home', query: {id:123456}}">? // query傳參數(shù) (類似get,url后面會顯示參數(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 ,第一次可請求,刷新頁面id會消失 // 配置path,刷新頁面id會保留 // html 取參 $route.params.id ? ?script 取參 this.$route.params.id
4.query和params區(qū)別
query類似get, 跳轉(zhuǎn)之后頁面url后面會拼接參數(shù),類似?id=123456, 非重要性的可以這樣傳, 密碼之類還是用params刷新頁面id還在
params類似post, 跳轉(zhuǎn)之后頁面url后面不會拼接參數(shù), 但是刷新頁面id會消失。
3:this.$router.replace()
用法同上,和第2個的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); ? // 在瀏覽器記錄中前進一步,等同于 history.forward() }
向前或者向后跳轉(zhuǎn)n個頁面,n可為正整數(shù)或負整數(shù)
ps : 區(qū)別
this.$router.push
跳轉(zhuǎn)到指定url路徑,并向history棧中添加一個記錄,點擊后退會返回到上一個頁面。
this.$router.replace
跳轉(zhuǎn)到指定url路徑,但是history棧中不會有記錄,點擊返回會跳轉(zhuǎn)到上個頁面 (直接替換當前頁面)。
this.$router.go(n)
向前或者向后跳轉(zhuǎn)n個頁面,n可為正整數(shù)或負整數(shù)。
總結
以上所述就是給大家介紹的vue路由跳轉(zhuǎn)四種方式,僅為個人經(jīng)驗。希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Vue?結合Sortablejs實現(xiàn)table行排序功能
在一個列表展示頁面上,使用了表格組件,原有組件本身不支持拖拽功能,需求要求在列表的基礎上支持行拖拽排序,因此引入了www.sortablejs.com插件,接下來通過本文給大家講解Vue?結合Sortablejs實現(xiàn)table行排序功能,需要的朋友可以參考下2022-10-10vue使用axios獲取不到響應頭Content-Disposition的問題及解決
這篇文章主要介紹了vue使用axios獲取不到響應頭Content-Disposition的問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06vue3+element-plus動態(tài)路由菜單示例代碼
這篇文章主要介紹了vue3+element-plus動態(tài)路由菜單示例代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-11-11vue項目中使用rimraf?dev啟動時刪除dist目錄方式
這篇文章主要介紹了vue項目中使用rimraf?dev啟動時刪除dist目錄方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04