iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >Vue实现路由跳转至外界页面
  • 400
分享到

Vue实现路由跳转至外界页面

Vue路由Vue路由跳转Vue路由跳转页面 2022-12-09 12:12:06 400人浏览 安东尼
摘要

目录Vue路由跳转至外界页面解决办法Vue路由跳转页面的几种方式总结Vue路由跳转至外界页面 用法 如果使用路由是在 vue 页面中来回跳转,可以使用 this.$router.pu

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.id

3.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.id

5.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}`)

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Vue实现路由跳转至外界页面

本文链接: https://www.lsjlt.com/news/174631.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作