javascript - 如何在不同的 vue 方法中调用函数内部的 vue 方法
问题描述
我试图在另一个名为 testing 的 vue 方法内的函数中调用一个名为“updateUserToPremium()”的 Vue 方法。我正在尝试这样做,因为“rp”是由支付提供商提供的 javascript 事件处理程序。
我也尝试过使用“this.updateUserToPremium(userId)”,但这并不能解决问题。我试图了解范围之间的关系,但我错过了一些东西。
methods: {
updateUserToPremium(userId) {
axios.post(`/user/${userId}`, {}, {
headers: { "Authorization": `Bearer ${this.$store.state.token}` }
}).then(res => {
console.log('Success session creation');
this.routeToStartpage()
}).catch(error => {
console.log(error);
return error;
})
},
testing(userId) {
var rp = new Reepay.ModalSubscription(sessionId);
rp.addEventHandler(Reepay.Event.Accept, function(data) {
console.log('Success', data);
updateUserToPremium(userId); // <--- This triggers updateUserToPremium is not defined'
rp.destroy();
})
},
}
解决方案
尝试这样的调用方法:
rp.addEventHandler(Reepay.Event.Accept, (data) => {
console.log('Success', data);
this.updateUserToPremium(userId); // <--- This triggers updateUserToPremium is not defined'
rp.destroy();
})
推荐阅读
- javascript - 智能排序(对可能包含或不包含数字的字符串进行排序)
- scala - 如何在将文件下沉到 playframework 中的目标之前检查文件大小?
- javascript - 在p5 js中沿运动方向旋转物体
- php - 如何在php中接收Soap响应
- reactjs - 如何检查从 ReactStrap 中选择了多选输入表单中的哪些选项
- algorithm - 长方体表面上两点之间的最短路径
- visual-studio - VsVim - 默认情况下不区分大小写的搜索
- angular - Angular6 - 如何检查Stripe支付是否成功
- java - 快速读取大量数据
- ruby-on-rails - 无法通过 Rails 5 中的 3 个模型访问字段