vue路由$router.push()的三种传参方式

$ router.push() 的三种传参方式
最近在工作中遇到需要使用$router.push传递一个对象,在这里做一下技术总结

通过path传参

很常用的一种,传递的参数会显示到path中,在刷新页面时数据不会丢失,常用于数据的新增、编辑、查看详情。

this.$router.push(`pushAdd/${id}`)

路由配置

{
                path: '/pushAdd/:id',
                component: () => import('@/views/$routePush/add'),
            },

path展示

在这里插入图片描述
参数获取

this.$route.params.id

通过params传参

不会显示在path上
页面刷新数据会丢失
通过路由name匹配路由

			let data={
                name:'zhangsan',
                age:18
            }
            this.$router.push({
                name:'RoutePushEdit',
                params:{
                    id:id,
                    data:data
                }
            })

路由配置

{
                path: '/pushEdit',
                name: 'RoutePushEdit',
                component: () => import('@/views/$routePush/edit'),
            },

参数获取

this.$route.params.id;
this.$route.params.data;

通过query传参

会显示在path上
页面刷新数据不丢失
通过路由name匹配路由

			let data={
                name:'zhangsan',
                age:18
            }
            this.$router.push({
                name:'RoutePushEdit',
                query:data
            })

路由配置

{
                path: '/pushEdit',
                name: 'RoutePushEdit',
                component: () => import('@/views/$routePush/edit'),
            },

path
在这里插入图片描述

参数获取

this.$route.query
  • 29
    点赞
  • 85
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Vue中使用this.$router.push方法传递数组对象,可以使用以下方式: 1. 使用params传递参数: 在定义事件中,使用this.$router.push方法传递参数对象,在目标组件中使用this.$route.params接收参数。 例如:this.$router.push({name: 'Home', params: {user: 'david', items: [1, 2, 3}}); 在目标组件的created或mounted生命周期钩子中,使用this.$route.params接收参数。 例如:const items = this.$route.params.items; 2. 使用query传递参数: 在定义事件中,使用this.$router.push方法传递参数对象,在目标组件中使用this.$route.query接收参数。 例如:this.$router.push({path: '/home', query: {user: 'david', items: [1, 2, 3}}); 在目标组件的created或mounted生命周期钩子中,使用this.$route.query接收参数。 例如:const items = this.$route.query.items; 需要注意的是,通过query方式传递的数组对象会在地址栏中被强制转换成字符串,刷新页面后无法获取到正确的数组对象值。因此,推荐使用params方式传递数组对象。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [vue中this.$router.push()路由传值和获取的两种常见方法](https://blog.csdn.net/Vivien_CC/article/details/126880417)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值