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项目如何引入bootstrap、elementUI、echarts
Nov 26 Vue.js
vue使用element-ui实现表单验证
Dec 13 Vue.js
vue 数据操作相关总结
Dec 17 Vue.js
基于vue+echarts数据可视化大屏展示的实现
Dec 25 Vue.js
Vue实现随机验证码功能
Dec 29 Vue.js
Vue仿Bibibili首页的问题
Jan 21 Vue.js
vue+springboot实现登录验证码
May 27 Vue.js
如何优化vue打包文件过大
Apr 13 Vue.js
解决vue-router的beforeRouteUpdate不能触发
Apr 14 Vue.js
vue/cli 配置动态代理无需重启服务的方法
May 20 Vue.js
vue递归实现树形组件
Jul 15 Vue.js
Vue router配置与使用分析讲解
Dec 24 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
用cookies来跟踪识别用户
2006/10/09 PHP
PHP操作XML作为数据库的类
2010/12/19 PHP
php入门学习知识点四 PHP正则表达式基本应用
2011/07/14 PHP
php中stream(流)的用法
2014/03/25 PHP
PHP中的Memcache详解
2014/04/05 PHP
thinkphp中html:list标签传递多个参数实例
2014/10/30 PHP
Thinkphp3.2.3整合phpqrcode生成带logo的二维码
2016/07/21 PHP
简单通用的JS滑动门代码
2008/12/19 Javascript
javascript RadioButtonList获取选中值
2009/04/09 Javascript
基于jQuery的左右滚动实现代码
2010/12/03 Javascript
kmock javascript 单元测试代码
2011/02/06 Javascript
浅谈javascript中的instanceof和typeof
2015/02/27 Javascript
js动态获取子复选项并设计全选及提交的实现方法
2016/06/24 Javascript
Vue项目history模式下微信分享爬坑总结
2019/03/29 Javascript
Vue如何将页面导出成PDF文件
2020/08/17 Javascript
Python实现从url中提取域名的几种方法
2014/09/26 Python
python 读写txt文件 json文件的实现方法
2016/10/22 Python
Python编程实现微信企业号文本消息推送功能示例
2017/08/21 Python
python二维列表一维列表的互相转换实例
2018/07/02 Python
python3基于OpenCV实现证件照背景替换
2018/07/18 Python
Python cv2 图像自适应灰度直方图均衡化处理方法
2018/12/07 Python
Python获取Redis所有Key以及内容的方法
2019/02/19 Python
在Python中等距取出一个数组其中n个数的实现方式
2019/11/27 Python
CSS3 background-image颜色渐变的实现代码
2018/09/13 HTML / CSS
美国百年历史早餐食品供应商:Wolferman’s
2017/01/18 全球购物
PHP引擎php.ini参数优化深入讲解
2021/03/24 PHP
公司行政经理岗位职责
2013/12/24 职场文书
出国签证在职证明
2014/01/16 职场文书
高中数学教学反思
2014/01/30 职场文书
应届毕业生应聘自荐信范文
2014/02/26 职场文书
数控技校生自我鉴定
2014/04/19 职场文书
单位消防安全责任书
2014/07/23 职场文书
政府班子四风问题整改措施
2014/10/04 职场文书
2014年终个人工作总结
2014/11/07 职场文书
个人原因辞职信模板
2015/05/13 职场文书
Pytorch实现图像识别之数字识别(附详细注释)
2021/05/11 Python