我使用的是支付api,其中有一个来自api的回调方法,它表示事务的状态。
async makePayment() {
this.$launchFlutterwave({
tx_ref: Date.now(),
amount: this.amount,
currency: "KES",
customer: {
email: "user@gmail.com",
phonenumber: this.user.phone_number,
name: this.user.name,
plot_unique_id: this.plot_unique_id
},
callback: function(data) {
console.log(data);
this.registerPayment(data);
},
customizations: {
title: "",
description: ",
logo: "https://assets.piedpiper.com/logo.png"
}
});
},
async registerPayment(data) {
console.log("hit");
await axios.post("/api/flutterwave/register/payment", data);
}
在回调中,我想注册一个方法
callback: function(data) {
console.log(data);
this.registerPayment(data);
},
然后,该方法将接收到的后台数据和其他特定于用户的数据发布到后端。
async registerPayment(data) {
console.log("hit");
await axios.post("/api/flutterwave/register/payment", data);
}
但是,当调用回调中的方法时,我将收到错误Uncaught TypeError: this.registerPayment is not a function
。
发布于 2021-10-19 09:57:43
这是因为回调内部的上下文是不同的:
callback: function(data) {
console.log(this); // you will see registerPayment doesnt exist here
this.registerPayment(data);
},
为什么不直接使用registerPayment
呢?你可以这样做:
callback: this.registerPayment
如果仍然希望从registerPayment
内部调用callback
,可以使用箭头函数访问外部上下文:
callback: (data) => {
this.registerPayment(data);
}
https://stackoverflow.com/questions/69628728
复制相似问题