vue?點擊按鈕?路由跳轉(zhuǎn)指定頁面的實現(xiàn)方式
點擊按鈕 路由跳轉(zhuǎn)指定頁面
最終效果
點擊指定按鈕,跳轉(zhuǎn)指定 /login 頁面
代碼:
<button @click="gotolink" class="btn btn-success">點擊跳轉(zhuǎn)頁面</button>
methods:{ gotolink(){ //點擊跳轉(zhuǎn)至上次瀏覽頁面 // this.$router.go(-1) //指定跳轉(zhuǎn)地址 this.$router.replace('/login') } }
vue跳轉(zhuǎn)頁面常用的方式
vue跳轉(zhuǎn)頁面有好幾種不同方法,下面將通過實例代碼給大家介紹,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下。
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 ,第一次可請求,刷新頁面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); ? // 在瀏覽器記錄中前進(jìn)一步,等同于 history.forward() }
向前或者向后跳轉(zhuǎn)n個頁面,n可為正整數(shù)或負(fù)整數(shù)
ps : 區(qū)別
this.$router.push
跳轉(zhuǎn)到指定url路徑,并向history棧中添加一個記錄,點擊后退會返回到上一個頁面。
this.$router.replace
跳轉(zhuǎn)到指定url路徑,但是history棧中不會有記錄,點擊返回會跳轉(zhuǎn)到上個頁面 (直接替換當(dāng)前頁面)。
this.$router.go(n)
向前或者向后跳轉(zhuǎn)n個頁面,n可為正整數(shù)或負(fù)整數(shù)。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue+element搭建后臺小總結(jié) el-dropdown下拉功能
這篇文章主要為大家詳細(xì)介紹了vue+element搭建后臺小總結(jié),el-dropdown下拉功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09vue-socket.io接收不到數(shù)據(jù)問題的解決方法
這篇文章主要介紹了解決vue-socket.io接收不到數(shù)據(jù)問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Vue使用Echarts實現(xiàn)橫向柱狀圖,并通過WebSocket即時通訊更新
這篇文章主要介紹了Vue使用Echarts實現(xiàn)橫向柱狀圖,并通過WebSocket即時通訊更新方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03