我正在为我的Checkout流程集成PayPal智能按钮下面是呈现按钮的代码
LoadPaypalButton(orderid :string , link : string,token : string , applicationbaseurl:string)
{
this.addPaypalScript().then(()=>{paypal.Buttons({enableStandardCardFields: true,
style: {
shape: 'rect',
color: 'blue',
layout: 'vertical',
label: 'paypal',
},
createOrder: function() {
return orderid
},
onApprove : function(data , actions) {
return fetch(link, {
method: 'post',
headers: {
'Authorization': token,
'content-type': 'application/json'
},
body: JSON.stringify({
OrderID: data.orderID
})
}).then(function(res) {
return res.json();
}).then(function(details) {
console.log( details);
console.log(details);
actions.redirect(applicationbaseurl+'OrderHistory/'+token);
})
}
}
).render('#paypal-button-container')});
}
一切工作正常,我想在事务完成后调用外部函数来处理我的UI,而无需将用户带到任何其他页面。如何在脚本中调用如下函数
callsuccess()
{
// Some Other work....
console.log("Something ..!");
}
我正在使用Angular 8.0作为前端,这是我到目前为止在OnApprove中尝试的方法:
onApprove : function(data , actions) {
return fetch(link, {
method: 'post',
headers: {
'Authorization': token,
'content-type': 'application/json'
},
body: JSON.stringify({
OrderID: data.orderID
})
}).then(function(res) {
return res.json();
}).then(function(details) {
this.callsuccess(); // This does not work
actions.redirect(applicationbaseurl+'OrderHistory/'+token);
})
}
给我下面的错误
zone-evergreen.js:172 Uncaught TypeError: Cannot read property 'callsuccess' of undefined
at http://localhost:4200/main.js:1430:30
at ZoneDelegate.invoke (http://localhost:4200/polyfills.js:3365:26)
at Zone.run (http://localhost:4200/polyfills.js:3130:43)
at http://localhost:4200/polyfills.js:3861:36
at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:3397:31)
at Zone.runTask (http://localhost:4200/polyfills.js:3174:47)
at drainMicroTaskQueue (http://localhost:4200/polyfills.js:3565:35)
有没有人建议实现这一目标..?
尝试使用箭头功能:
onApprove : (data , actions) => {
return fetch(link, {
method: 'post',
headers: {
'Authorization': token,
'content-type': 'application/json'
},
body: JSON.stringify({
OrderID: data.orderID
})
}).then((res) => {
return res.json();
}).then((details) => {
this.callsuccess(details);
actions.redirect(applicationbaseurl+'OrderHistory/'+token);
})
}
请注意,承诺的功能如何function(data , actions) {
变得(data, actions) => {
一样then
。
这应该保留this
as箭头函数具有this
关键字的词法范围的情况。此功能也称为lambda functions
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句