Vue實現(xiàn)路由跳轉(zhuǎn)至外界頁面
Vue路由跳轉(zhuǎn)至外界頁面
用法
如果使用路由是在 vue 頁面中來回跳轉(zhuǎn),可以使用 this.$router.push() 實現(xiàn),但是如果想用這種方法跳轉(zhuǎn)到外部鏈接就會報錯,因為外部頁面中是存在 HTTP 等前綴的。
解決辦法
1. 在 data 中定義好要跳轉(zhuǎn)的外部鏈接
data() {
?? ?return {
?? ??? ?url: 'http://www.baidu.com'
?? ?}
}2. 按鈕中創(chuàng)建單擊事件
<button @click='routeClick(url)'></button>
3. 函數(shù)實現(xiàn)
method: {
?? ?routeClick(e) {
?? ??? ?// 通過此方法可以使用
?? ??? ?window.location.href = e;
?? ?}
}Vue路由跳轉(zhuǎn)頁面的幾種方式
1.聲明式導(dǎo)航router-link
// 注意:router-link中鏈接如果是'/'開始就是從根路由開始,如果開始不帶'/',則從當(dāng)前路由開始。
<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"> ?
//傳遞對象
<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.id3.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.直接通過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.id5.this.$router.go(n)
向前或者向后跳轉(zhuǎn)n個頁面,n可為正整數(shù)或負整數(shù)
6.跳轉(zhuǎn)頁面打開新窗口并攜帶參數(shù)
const routeData = this.$router.resolve({
? ? ? ? ? ? ? ? path: `/workbench/customer_detail/${this.audioFrom.import_id}`
? ? ? ? ? ? })
window.open(routeData.href, '_blank')7.跳轉(zhuǎn)新項目并攜帶參數(shù)
window.open(`https://hao123/#/workbench/customer_detail/${this.audioFrom.import_id}`)總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3.x如何操作v-html指令中HTML的DOM和樣式
在 Vue3.x 中,v-html 指令用于將 HTML 字符串渲染為真實的 DOM 元素,下面我們來看看具體如何操作v-html指令中HTML的DOM和樣式吧2025-04-04
vue3使用Element-plus的el-pagination分頁組件時無法顯示中文
本文主要介紹了vue3使用Element-plus的el-pagination分頁組件時無法顯示中文,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
vue服務(wù)器代理proxyTable配置如何解決跨域
這篇文章主要介紹了vue服務(wù)器代理proxyTable配置如何解決跨域問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
element中el-autocomplete的常見用法示例
這篇文章主要給大家介紹了關(guān)于element中el-autocomplete的常見用法的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用element具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-03-03

