“this”在wepay回调closuer中返回undefined

时间:2018-06-13 11:37:07

标签: vue.js nuxt.js wepay

我需要你的帮助。我试图调用它来访问我在vuejs中的数据对象中的变量,但是这个返回未定义

这是我的代码

let response = ''
        let self  = this

        response = WePay.credit_card.create({
          "client_id":        118711,
          "user_name":        this.cardDetails.name,
          "email":            this.cardDetails.email,
          "cc_number":        this.cardDetails.number,
          "cvv":              this.cardDetails.cvc,
          "expiration_month": this.cardDetails.month,
          "expiration_year":  this.cardDetails.year,
          "address": {
              "postal_code": this.cardDetails.zipcode
          }
        }, function(data, self) {
          if (data.error) {
            // self.loader = false
            console.log(window)
            this.loader = false
            this.emitErrorNotify({message: data.error_description})
            // handle error response
          } else {
            console.log(data)
              // call your own app's API to save the token inside the data;
              // show a success page
          }
        })

任何人都可以帮我解决它是怎么做的吗?谢谢和问候。

1 个答案:

答案 0 :(得分:2)

您定义

let self  = this
顶部, 但在function(data, self)内部会被覆盖。更改其中一个变量并使用它而不是这个。 或者将this绑定到函数,以便在以下内容中使用它:

function(data, self) {

}.bind(this)