vue.js 操作数据常见报错 ...split is not a function...

在做数组字符串等接口返回数据操作中经常会遇到这样的错误:vue.js 操作数据常见报错 ...split is not a function...

而实际运行代码又可以正常运行并且能够正常操作数据结果也是对的。那想要去掉这种报错怎么解决呢:

实际后端返回很多数据看似是字符串,其实是个数组类型。所以只要转换成字符串即可:

 formatTreeDate(data) {
      if (!data) return;
      var str = data.complainType;
      console.log(typeof str)// obj '1,2' 看着是字符串其实是数组
      str = JSON.stringify(str)
      console.log(typeof str)// str 转成真正的字符串就不会报错了
      if (str) {
        str = str.split(",");
        this.form.complainType = str;
      }
      console.log(this.form.complainType);
    },

这种问题常遇到后端接收过来是字符串对象的情况下

posted @ 2021-07-14 16:03  少哨兵  阅读(11063)  评论(0编辑  收藏  举报