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 组件注册
Nov 20 Vue.js
解决Vue-cli3没有vue.config.js文件夹及配置vue项目域名的问题
Dec 04 Vue.js
vue 在服务器端直接修改请求的接口地址
Dec 19 Vue.js
vue使用vue-quill-editor富文本编辑器且将图片上传到服务器的功能
Jan 13 Vue.js
vue实现简易计算器功能
Jan 20 Vue.js
vue form表单post请求结合Servlet实现文件上传功能
Jan 22 Vue.js
Vue 事件的$event参数=事件的值案例
Jan 29 Vue.js
Vue实现动态查询规则生成组件
May 27 Vue.js
vue+elementui 实现新增和修改共用一个弹框的完整代码
Jun 08 Vue.js
详解Vue router路由
Nov 20 Vue.js
vue实现移动端div拖动效果
Mar 03 Vue.js
vue 自定义组件添加原生事件
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 将图片按创建时间进行分类存储的实现代码
2010/01/05 PHP
PHP中call_user_func_array()函数的用法演示
2012/02/05 PHP
php简单实现多字节字符串翻转的方法
2015/03/31 PHP
php通过两层过滤获取留言内容的方法
2016/07/11 PHP
PHP类型约束用法示例
2016/09/28 PHP
浏览器脚本兼容 文本框中,回车键触发事件的兼容
2010/06/21 Javascript
javascript中全局对象的parseInt()方法使用介绍
2013/12/19 Javascript
jQuery 文本框得失焦点的简单实例
2014/02/19 Javascript
php读取sqlite数据库入门实例代码
2014/06/25 Javascript
jQuery横向擦除焦点图特效代码分享
2015/09/06 Javascript
JavaScript 中有关数组对象的方法(详解)
2016/08/15 Javascript
详解node.js平台下Express的session与cookie模块包的配置
2017/04/26 Javascript
JQuery Ajax 异步操作之动态添加节点功能
2017/05/24 jQuery
微信小程序实现无限滚动列表
2020/05/29 Javascript
vue使用代理解决请求跨域问题详解
2019/07/24 Javascript
Python验证企业工商注册码
2015/10/25 Python
Python识别快递条形码及Tesseract-OCR使用详解
2019/07/15 Python
python 列表、字典和集合的添加和删除操作
2019/12/16 Python
tensorflow使用range_input_producer多线程读取数据实例
2020/01/20 Python
django model object序列化实例
2020/03/13 Python
Python3 pickle对象串行化代码实例解析
2020/03/23 Python
Selenium常见异常解析及解决方案示范
2020/04/10 Python
Django实现随机图形验证码的示例
2020/10/15 Python
html5使用canvas画一条线
2014/12/15 HTML / CSS
使用phonegap进行本地存储的实现方法
2017/03/31 HTML / CSS
倩碧美国官网:Clinique美国
2016/07/20 全球购物
Christys’ Hats官网:英国帽子制造商
2018/11/28 全球购物
飞利浦法国官网:Philips法国
2019/07/10 全球购物
上班玩游戏检讨书
2014/02/07 职场文书
交通事故一次性赔偿协议书范本
2014/11/02 职场文书
订货会邀请函
2015/01/31 职场文书
布达拉宫导游词
2015/02/02 职场文书
2016幼儿园新学期寄语
2015/12/03 职场文书
JavaScript 去重和重复次数统计
2021/03/31 Javascript
Redis6.0搭建集群Redis-cluster的方法
2021/05/08 Redis
Python 实现绘制子图及子图刻度的变换等问题
2021/05/31 Python