vue 多次嵌套使用this.$options.methods 导致methods of undefined

问题描述

在compare 中使用this.$options.methods 然后在formatDataPlus里面再次使用this.$options.methods导致报错,

分析原因,就是因为,this指向发生错误,导致报错methods of undefined。所以只需要更改指向即可。

 
compare (e) {
      if (this.composition1 === '' || this.composition2 === '' || this.composition1 === this.composition2) {
        this.$message({
          message: '请选择合适的选项',
          type: 'warning'
        })
      } else {
        this.$axios({
          url: '/api/classifyCompare',
          method: 'post',
          headers: {'X-CSRFToken': this.getCookie('csrftoken')},
          data: {
            c1: this.composition1,
            c2: this.composition2
          }
        }).then(response => {
          console.log(response.data)
          let data = response.data
          let that = this
          this.table1 = this.$options.methods.formatDataPlus(JSON.stringify(data), that)
          this.table2 = this.$options.methods.formatDataPlus2(JSON.stringify(data))
          console.log('加载完毕')
        }).catch(error => {
          console.log('fail')
          console.log(error.response)
        })
      }
    },
formatDataPlus (da, that) {
      try {
        let data = JSON.parse(da)
        let c1 = data.c1
        let c2 = data.c2
        console.log(typeof c1)
        // eslint-disable-next-line no-unreachable
        c1 = JSON.parse(c1)
        // eslint-disable-next-line camelcase
        var c1_val = Object.values(c1)
        // eslint-disable-next-line no-use-before-define
        console.log(c2_val)
        c2 = JSON.parse(c2)
        // eslint-disable-next-line camelcase
        var c2_val = Object.values(c2)
        let key = Object.keys(c1)
        console.log(c1, c2, c1_val)
        console.log(key)
        let arr = []
        console.log('option:' + that.$options.methods)
        for (var i = 0; i < key.length; i++) {
          arr.push({c0: that.$options.methods.rename(key[i], key), c1: c1_val[i], c2: c2_val[i]})
        }
        console.log(arr)
        return arr
      } catch (e) {
        console.log(e)
      }
    },
    rename (data, keys) {
      let arr = ['ID', '中文名', '拼音', '拉丁语', '英文', '俗名', '生活型', '株', '根', '茎', '叶', '花', '果', '产地', '分布', '生活环境', '物候期', '药材性味', '功能作用']
      for (var i = 0; i < arr.length; i++) {
        if (data === keys[i]) {
          return arr[i]
        }
      }
    },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值