Vue路由传参(使用query传参页面刷新数据丢失问题)

1. 路由传参的两种方式

1. params传参(刷新页面数据会丢失)

 this.$router.push({
    
    
        name:'xxx',
        params:{
    
    
          id:id
        }
      })
  
//接收参数:
this.$route.params.id

2. query传参(刷新数据不会丢失)

this.$router.push({
    
    
        path:'/xxx',
        query:{
    
    
          id:id
        }
      })
  
//接收参数:
this.$route.query.id

2. 使用query传参页面刷新数据丢失问题

注意

在query传参的时候可能会遇到一个问题:当传递参数为对象时,第一次跳转数据是没问题的,但第二次刷新页面数据会变为[object object]

解决办法:
要跳转的传参页:先将数组转换为字符串。

detailFun(row) {
    
    
      //console.log(JSON.stringify(row))
      this.$router.push({
    
    
        path: '/product',
        query: {
    
     productDetail: JSON.stringify(row) }
      })
},

接收页面:

 created() {
    
    
 	this.orderDetail = JSON.parse(this.$route.query.productDetail)
 }

这样转换一下刷新后数据就不会丢失。

猜你喜欢

转载自blog.csdn.net/qq_58648235/article/details/130062893