[因此,我尝试从api上的回调响应中为付款应用程序razorpay调用函数。我不能使用'this'来调用组件内的函数,因为它位于处理程序中的嵌套函数内。如何从回调“ handler”调用函数handle_response()?
myComponent.ts
var options = {
"amount": 100,
"name": "ABC",
"currency": "USD",
"handler": function (response){
console.log(response);//this returns the expected value
this.handle_response(response); //does not work as cannot identify 'this'
}
};
var rzp1 = new this.winRef.nativeWindow.Razorpay(options);
rzp1.open();
handle_response(_response){....}
您想使用函数的bind
方法或打字稿中的粗箭头语法。可能是:
let options = {
"amount": 100,
"name": "ABC",
"currency": "USD",
"handler": function (response){
console.log(response);//this returns the expected value
this.handle_response(response); //does not work as cannot identify 'this'
}.bind(this)
};
let rzp1 = new this.winRef.nativeWindow.Razorpay(options);
rzp1.open();
handle_response(_response){....}
OR
let options = {
"amount": 100,
"name": "ABC",
"currency": "USD",
"handler": (response) => {
console.log(response);//this returns the expected value
this.handle_response(response); //does not work as cannot identify 'this'
}
};
let rzp1 = new this.winRef.nativeWindow.Razorpay(options);
rzp1.open();
handle_response(_response){....}