vue路由用query方式传参(参数中有对象),刷新后传入的对象为'[Object Object]'

原始代码:

// 当前页面路由
    handleDetail(row, type) {
      this.$router.push({
        path: '/dependencies/detail',
        query: {
          detailData: row, // row为一个对象
          type: type
        }
      })
    }
// 跳转到此页面
 created() {
    this.type = this.$route.query.type
    this.detailData =this.$route.query.detailData  // 刷新后,this.detailData='[Object Object]'
 }

解决方法:使用JSON.stringify()将要传的对象转为字符串,接收时使用JSON.parse()转为对象
现在代码:

  handleDetail(row, type) {
     this.$router.push({
       path: '/dependencies/detail',
       query: {
         detailData: JSON.stringify(row),
         type: type
       }
     })
   }
  created() {
    this.type = this.$route.query.type
    this.detailData =JSON.parse(this.$route.query.detailData)
  }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容