vue route新窗口跳转页面并且携带与接收参数


Posted in Vue.js onApril 10, 2022

1、携带普通类型参数

字符串、数字等。

path:要跳转新页面的路由链接

query:要携带的参数

let pathInfo = this.$router.resolve({
  path:'/product_detail',
     query:{
         productId:'11'
     }
 })
 window.open(pathInfo.href, '_blank');

新页面的参数接收:

this.productId = this.$route.query.productId

2、携带复杂类型参数

对象、数组等,通过JSON转换进行传递。

let pathInfo = this.$router.resolve({
   path:'/product_detail',
     query:{
         data:{name:'张三'}
     }
 })
 window.open(pathInfo.href, '_blank');

新页面的参数接收:

console.log(this.$route.query.data)

vue页面跳转并传参的八种方式

我们知道,在vue中每个页面都需要在路由中声明,就是在router/index.js中写下面代码:

import Vue from 'vue'
import Router from 'vue-router'
import Test from "../components/Test";
Vue.use(Router)
export default new Router({
  mode: 'history',
  routes: [
          {
              path: '/t',
              name: 'Test',
              component: Test,
              hidden:true
            },
        ]
    })

实现页面跳转并传参有多种方式:

方法一

在template中可以使用<router-link>标签实现跳转,跳转的路径是http://localhost:8080/t?index=id,如下:

<router-link to="/t?index=1">
     <button class="btn btn-default">点击跳转</button>
</router-link>

只需要点击按钮就可以实现跳转,不需要写js代码,需要传递参数的话只需要/t?index=1即可,这样的话跳转的页面获取参数通过window.location.href能够获取到完整的url,然后截取参数。也可以通过下面代码获取参数

this.$route.query.index

方法二

跳转的路径是http://localhost:8080/t?index=id

<router-link :to="{path:'/t',query: {index: 1}}">
     <button class="btn btn-default">点击跳转</button>
</router-link>

其中需要注意,这里的to前面一定要加冒号,path的值要和上面路由定义的值一致,传参用query,里面是参数字典。

接收参数:

this.$route.query.index

方法三

命名路由的方式:

跳转的路径是http://localhost:8080/t?index=id

<router-link :to="{name:'Test',params: {index: 1}}">
     <button class="btn btn-default">点击跳转</button>
</router-link>

注意这里的name也要和router/index.js中声明的name值一致,并且传参使用params,和name配对的是params,和path配对的是query。

接收参数:

this.$route.params.index

方法四

跳转的路径是http://localhost:8080/t/id

<router-link:to="'/test/'+1">
     <button class="btn btn-default">点击跳转</button>
</router-link>

这时的路由也需要更为为下面的形式:

routes: [
          {
              path: '/t/:index',
              name: 'Test',
              component: Test,
              hidden:true
            },
        ]

接收参数:

this.$route.params.index

方法五

上面四种方法都是在html中实现的跳转,还有另外对应的在js中实现的跳转并传参的方法,代码如下:

<template>
<button @click = "func()">跳转</button>
</template>
<script>
    export default{
        methods:{
            func (){
                this.$router.push({path: '/t?index=1'});
            }
        }
    }
</script>

接收参数依然使用

this.$route.query.index

方法六

<template>
<button @click = "func()">跳转</button>
</template>
<script>
    export default{
        methods:{
            func (){
                this.$router.push({path: '/t',query:{ index:'1'}});
            }
        }
    }
</script>

接收参数依然使用

this.$route.query.index

方法七

<template>
<button @click = "func()">跳转</button>
</template>
<script>
    export default{
        methods:{
            func (){
                this.$router.push({path: '/t/index'});
            }
        }
    }
</script>

接收参数依然使用

this.$route.query.index

方法八

<template>
<button @click = "func()">跳转</button>
</template>
<script>
    export default{
        methods:{
            func (){
                this.$router.push({name: 'Test',params:{ index:'1'}});
            }
        }
    }
</script>

接收参数依然使用

this.$route.params.index

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

Vue.js 相关文章推荐
vue-calendar-component 封装多日期选择组件的实例代码
Dec 04 Vue.js
浅谈Vue使用Elementui修改默认的最快方法
Dec 05 Vue.js
Vue——前端生成二维码的示例
Dec 19 Vue.js
vue 使用class创建和清除水印的示例代码
Dec 25 Vue.js
vue element el-transfer增加拖拽功能
Jan 15 Vue.js
Vue 数据响应式相关总结
Jan 28 Vue.js
vue 使用饿了么UI仿写teambition的筛选功能
Mar 01 Vue.js
vue+django实现下载文件的示例
Mar 24 Vue.js
Vue项目打包、合并及压缩优化网页响应速度
Jul 07 Vue.js
vue3使用vuedraggable实现拖拽功能
Apr 06 Vue.js
vue elementUI表格控制对应列
Apr 13 Vue.js
vue组件vue-esign实现电子签名
Apr 21 Vue.js
vue实力踩坑之push当前页无效
Apr 10 #Vue.js
vue实现Toast组件轻提示
Apr 10 #Vue.js
vue自定义右键菜单之全局实现
Apr 09 #Vue.js
vue判断按钮是否可以点击
Apr 09 #Vue.js
VUE之图片Base64编码使用ElementUI组件上传
Apr 09 #Vue.js
vue如何实现关闭对话框后刷新列表
Apr 08 #Vue.js
vue实现列表垂直无缝滚动
Apr 08 #Vue.js
You might like
创建配置文件 用PHP写出自己的BLOG系统 2
2010/04/12 PHP
php中json_decode()和json_encode()的使用方法
2012/06/04 PHP
PHP开源开发框架ZendFramework使用中常见问题说明及解决方案
2014/06/12 PHP
laravel 5 实现模板主题功能
2015/03/02 PHP
PHP排序算法类实例
2015/06/17 PHP
php把数组值转换成键的方法
2015/07/13 PHP
PHP/ThinkPHP实现批量打包下载文件的方法示例
2017/07/31 PHP
PHP使用curl_multi实现并发请求的方法示例
2018/04/29 PHP
jMessageBox 基于jQuery的窗口插件
2009/12/09 Javascript
JavaScript高级程序设计 DOM学习笔记
2011/09/10 Javascript
Javascript编程中几种继承方式比较分析
2015/11/28 Javascript
Jquery easyui开启行编辑模式增删改操作
2016/01/14 Javascript
js轮播图代码分享
2016/07/14 Javascript
如何检测JavaScript的各种类型
2016/07/30 Javascript
使用grunt合并压缩js和css文件的方法
2017/03/02 Javascript
jQuery实现火车票买票城市选择切换功能
2017/09/15 jQuery
使用Vue自定义指令实现Select组件
2018/05/24 Javascript
vue组件中iview的modal组件爬坑问题之modal的显示与否应该是使用v-show
2019/04/12 Javascript
vue中datepicker的使用教程实例代码详解
2019/07/08 Javascript
Js代码中的span拼接问题解决
2019/11/22 Javascript
jquery.validate自定义验证用法实例分析【成功提示与择要提示】
2020/06/06 jQuery
[53:52]OG vs EG 2018国际邀请赛淘汰赛BO3 第二场 8.23
2018/08/24 DOTA
python网络编程之文件下载实例分析
2015/05/20 Python
详解Python各大聊天系统的屏蔽脏话功能原理
2016/12/01 Python
解决Mac安装scrapy失败的问题
2018/06/13 Python
python命令行工具Click快速掌握
2019/07/04 Python
python生成器推导式用法简单示例
2019/10/08 Python
Python中base64与xml取值结合问题
2019/12/22 Python
python接口自动化如何封装获取常量的类
2019/12/24 Python
python爬虫 requests-html的使用
2020/11/30 Python
求最大连续递增数字串(如"ads3sl456789DF3456ld345AA"中的"456789")
2015/09/11 面试题
Java基础面试题
2014/07/19 面试题
教师岗位聘任书范文
2014/03/29 职场文书
作文批改评语大全
2014/04/23 职场文书
投诉书格式范本
2015/07/02 职场文书
公司环境卫生管理制度
2015/08/05 职场文书