记录vue路由传递对象

本文分享了在Vue中正确传递和接收对象参数的方法,避免使用不当导致的[object Object]显示错误及JSON解析错误,强调了使用JSON.stringify、JSON.parse结合encodeURIComponent和decodeURIComponent的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(ps:此条仅作为个人记录)

vue路由传递一个对象作为参数的时候,不使用JSON.stringify和JSON.parse,接收参数的页面刷新后,接收的对象会变为[object Object]

 

  • 错误示范1:(未使用JSON.stringify和JSON.parse)
//传参页面
look(row){
 this.$router.push({
       path: './createProject',
       query:{allData: row}   //row是一个对象
 })
}



//接收参数页面
if (this.$route.query.allData) {
   console.log(this.$route.query.allData); 
}

第一次未刷新,打印结果(是正常的)

第二次刷新页面打印结果

   

 

 

所以使用JSON.stringify和JSON.parse

  • 错误示范2(使用了JSON.stringify和JSON.parse,未使用decodeURIComponent)
//传参页面
look(row){
    let objData = JSON.stringify(row)  //row是一个对象
        this.$router.push({
        path: './createProject',
        query:{allData: objData}
    })
}


//接收参数页面
if (this.$route.query.allData) {
   let abc = JSON.parse(this.$route.query.allData)
   console.log(abc)
}

结果报错:

 

  • 解决办法:(使用decodeURIComponent)

//传参页面
look(row){
    let objData = JSON.stringify(row)
    this.$router.push({
        path: './createProject',
        query:{allData: encodeURIComponent(objData)}
    })
}


//接收参数页面
if (this.$route.query.allData) {
    let formObj = decodeURIComponent(this.$route.query.allData)
    this.formLabelAlign = JSON.parse(formObj)
}

 

 

此微博经验学习于https://blog.youkuaiyun.com/L_eiDigaga/article/details/100315655

 

 

 

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值