Vue實(shí)現(xiàn)路由跳轉(zhuǎn)至外界頁(yè)面
Vue路由跳轉(zhuǎn)至外界頁(yè)面
用法
如果使用路由是在 vue 頁(yè)面中來(lái)回跳轉(zhuǎn),可以使用 this.$router.push() 實(shí)現(xiàn),但是如果想用這種方法跳轉(zhuǎn)到外部鏈接就會(huì)報(bào)錯(cuò),因?yàn)橥獠宽?yè)面中是存在 HTTP 等前綴的。
解決辦法
1. 在 data 中定義好要跳轉(zhuǎn)的外部鏈接
data() { ?? ?return { ?? ??? ?url: 'http://www.baidu.com' ?? ?} }
2. 按鈕中創(chuàng)建單擊事件
<button @click='routeClick(url)'></button>
3. 函數(shù)實(shí)現(xiàn)
method: { ?? ?routeClick(e) { ?? ??? ?// 通過(guò)此方法可以使用 ?? ??? ?window.location.href = e; ?? ?} }
Vue路由跳轉(zhuǎn)頁(yè)面的幾種方式
1.聲明式導(dǎo)航router-link
// 注意:router-link中鏈接如果是'/'開(kāi)始就是從根路由開(kāi)始,如果開(kāi)始不帶'/',則從當(dāng)前路由開(kāi)始。 <router-link :to="{name:'home'}"> ? <router-link :to="{path:'/home'}"> //name,path都行, 建議用name?
1.2
<router-link :to="{name:'home', params: {id:1}}"> <router-link :to="{name:'home', query: {id:1}}"> ? <router-link :to="/home/:id"> ? //傳遞對(duì)象 <router-link :to="{name:'detail', query: {item:JSON.stringify(obj)}}"></router-link>?
2.編程式導(dǎo)航 this.$router.push()
不帶參數(shù) this.$router.push('/home') this.$router.push({name:'home'}) this.$router.push({path:'/home'} 帶參數(shù) query傳參 1.路由配置: name: 'home', path: '/home' 2.跳轉(zhuǎn): this.$router.push({name:'home',query: {id:'1'}}) this.$router.push({path:'/home',query: {id:'1'}}) 3.獲取參數(shù) html取參: $route.query.id script取參: this.$route.query.id
3.params傳參
1.路由配置: name: 'home', path: '/home/:id'(或者path: '/home:id') 2.跳轉(zhuǎn): this.$router.push({name:'home',params: {id:'1'}}) 注意: // 只能用 name匹配路由不能用path // params傳參數(shù)(類似post) 路由配置 path: "/home/:id" 或者 path: "/home:id"否則刷新參數(shù)消失 3.獲取參數(shù) html取參:$route.params.id script取參:this.$route.params.id
4.直接通過(guò)path傳參
1.路由配置: name: 'home', path: '/home/:id' 2.跳轉(zhuǎn): this.$router.push({path:'/home/123'}) 或者: this.$router.push('/home/123') 3.獲取參數(shù): this.$route.params.id
5.this.$router.go(n)
向前或者向后跳轉(zhuǎn)n個(gè)頁(yè)面,n可為正整數(shù)或負(fù)整數(shù)
6.跳轉(zhuǎn)頁(yè)面打開(kāi)新窗口并攜帶參數(shù)
const routeData = this.$router.resolve({ ? ? ? ? ? ? ? ? path: `/workbench/customer_detail/${this.audioFrom.import_id}` ? ? ? ? ? ? }) window.open(routeData.href, '_blank')
7.跳轉(zhuǎn)新項(xiàng)目并攜帶參數(shù)
window.open(`https://hao123/#/workbench/customer_detail/${this.audioFrom.import_id}`)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vue跳轉(zhuǎn)頁(yè)面的幾種常用方法代碼示例
- vue實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)和參數(shù)傳遞的兩種方式
- 解決vue項(xiàng)目跳轉(zhuǎn)同樣的頁(yè)面不刷新的問(wèn)題思路詳解
- Vue路由跳轉(zhuǎn)傳參或打開(kāi)新頁(yè)面跳轉(zhuǎn)的方法總結(jié)
- vue3頁(yè)面跳轉(zhuǎn)的兩種方式
- vue中如何攜帶參數(shù)跳轉(zhuǎn)頁(yè)面
- vue項(xiàng)目中路由跳轉(zhuǎn)頁(yè)面不變問(wèn)題及解決
- vue?點(diǎn)擊按鈕?路由跳轉(zhuǎn)指定頁(yè)面的實(shí)現(xiàn)方式
- vue跳轉(zhuǎn)頁(yè)面打開(kāi)新窗口,并攜帶與接收參數(shù)方式
- vue如何跳轉(zhuǎn)到其他頁(yè)面
相關(guān)文章
詳解Vue webapp項(xiàng)目通過(guò)HBulider打包原生APP
這篇文章主要介紹了詳解Vue webapp項(xiàng)目通過(guò)HBulider打包原生APP,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06淺談Vue static 靜態(tài)資源路徑 和 style問(wèn)題
這篇文章主要介紹了淺談Vue static 靜態(tài)資源路徑 和 style問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11vue動(dòng)態(tài)顯示圖片報(bào)錯(cuò)404的解決
這篇文章主要介紹了vue動(dòng)態(tài)顯示圖片報(bào)錯(cuò)404的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07vue 項(xiàng)目build錯(cuò)誤異常的解決方法
這篇文章主要介紹了vue build錯(cuò)誤異常的解決方法 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04Vue中的Object.defineProperty全面理解
這篇文章主要介紹了Vue中的Object.defineProperty全面理解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue之webpack -v報(bào)錯(cuò)解決方案總結(jié)
這篇文章主要介紹了vue之webpack -v報(bào)錯(cuò)解決方案總結(jié),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09Vue axios 跨域請(qǐng)求無(wú)法帶上cookie的解決
這篇文章主要介紹了Vue axios 跨域請(qǐng)求無(wú)法帶上cookie的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09