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

这篇文章主要介绍了Vue实现路由跳转至外界页面方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Vue路由跳转至外界页面

用法

如果使用路由是在 vue 页面中来回跳转,可以使用 this.$router.push() 实现,但是如果想用这种方法跳转到外部链接就会报错,因为外部页面中是存在 HTTP 等前缀的。

解决办法

1. 在 data 中定义好要跳转的外部链接

data() {     return {         url: 'http://www.baidu.com'     } }

2. 按钮中创建单击事件

3. 函数实现

method: {     routeClick(e) {         // 通过此方法可以使用         window.location.href = e;     } }

Vue路由跳转页面的几种方式

1.声明式导航router-link

// 注意:router-link中链接如果是'/'开始就是从根路由开始,如果开始不带'/',则从当前路由开始。     //name,path都行, 建议用name 

1.2

       //传递对象  

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

总结

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

以上就是Vue实现路由跳转至外界页面的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Vue.js 教程