Vue实现路由跳转至外界页面
Vue路由跳转至外界页面
用法
如果使用路由是在 vue 页面中来回跳转,可以使用 this.$router.push() 实现,但是如果想用这种方法跳转到外部链接就会报错,因为外部页面中是存在 HTTP 等前缀的。
解决办法
1. 在 data 中定义好要跳转的外部链接
data() {
return {
url: 'http://www.baidu.com'
}
}2. 按钮中创建单击事件
<button @click='routeClick(url)'></button>
3. 函数实现
method: {
routeClick(e) {
// 通过此方法可以使用
window.location.href = e;
}
}Vue路由跳转页面的几种方式
1.声明式导航router-link
// 注意:router-link中链接如果是'/'开始就是从根路由开始,如果开始不带'/',则从当前路由开始。
<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.编程式导航 this.$router.push()
不带参数
this.$router.push('/home')
this.$router.push({name:'home'})
this.$router.push({path:'/home'}
带参数 query传参
1.路由配置:
name: 'home',
path: '/home'
2.跳转:
this.$router.push({name:'home',query: {id:'1'}})
this.$router.push({path:'/home',query: {id:'1'}})
3.获取参数
html取参: $route.query.id
script取参: this.$route.query.id3.params传参
1.路由配置:
name: 'home',
path: '/home/:id'(或者path: '/home:id')
2.跳转:
this.$router.push({name:'home',params: {id:'1'}})
注意:
// 只能用 name匹配路由不能用path
// params传参数(类似post) 路由配置 path: "/home/:id" 或者 path: "/home:id"否则刷新参数消失
3.获取参数
html取参:$route.params.id
script取参:this.$route.params.id
4.直接通过path传参
1.路由配置:
name: 'home',
path: '/home/:id'
2.跳转:
this.$router.push({path:'/home/123'})
或者:
this.$router.push('/home/123')
3.获取参数:
this.$route.params.id5.this.$router.go(n)
向前或者向后跳转n个页面,n可为正整数或负整数
6.跳转页面打开新窗口并携带参数
const routeData = this.$router.resolve({
path: `/workbench/customer_detail/${this.audioFrom.import_id}`
})
window.open(routeData.href, '_blank')7.跳转新项目并携带参数
window.open(`https://hao123/#/workbench/customer_detail/${this.audioFrom.import_id}`)总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
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分页组件时无法显示中文,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2023-12-12
element中el-autocomplete的常见用法示例
这篇文章主要给大家介绍了关于element中el-autocomplete的常见用法的相关资料,文中通过实例代码介绍的非常详细,对大家学习或者使用element具有一定的参考学习价值,需要的朋友可以参考下2023-03-03


最新评论