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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

南独酌酒nvn   2022-12-09 我要评论

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

总结

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们