我是Angular的新手,我的问题是:
我正在尝试通过使用Angular11将苹果支付集成到一个网站中。现在我已经安装了一个npm包,它包含了苹果支付JS (@.But/applepayjs)函数的类型定义,现在我不能从这个api-function访问我的函数。
我在api-function中的所有函数或变量都变成了"any“类型,并显示为未定义。我必须将数据发送到后端服务器,以便与Apple通信,并从服务器获得返回消息。我想知道是因为作用域的原因还是语言的原因。
我可以从第三方定义函数调用我的函数,或者通过构造函数访问依赖注入服务吗?如果有任何解决方案或建议?
请多多帮助,非常感谢。
这是我的代码。component.ts
export class ApplepayComponent {
constructior(
private service: ApplepayService
){}
// ...........
doApplePay(){
const paymentRequest: ApplePayJs.ApplePayPaymentRequest = {
countryCode: 'US',
currencyCode: 'USD',
supportedNetworks: ['visa', 'masterCard'],
merchantCapabilities: ['supports3DS'],
total: { label: 'My Store', amount: '10.00' }
}
const session = new ApplePaySession(2, paymentRequest);
session.onvalidatemerchant = function(event) {
const validationURL = event.validationURL;
// here is my question
// I want to call my service to communicate with back-end server
this.service.validateApplePay(this.secretKey).then((result) =>{
const merchantValidation = result.DATA.RETURN_MSG;
// and if complete, call
session.completeMerchantValidation(result.RETURN_MSG)
});
}
}
}
service.ts
export class ApplepayService {
constructior(
private service: ApiService
){}
async validateApplePay(secretKey: string):Promise<any>{
const res = await this.apiService.valdateApplepay({
SECRET_KEY: secretKey
}).toPromise();
return res;
}
}
发布于 2021-06-29 03:49:45
发生这种情况是因为api-function
中的上下文变量this
不可用。试试这个吧。
将您的function
转换为fat arrow function
。
session.onvalidatemerchant = (event) => { ... }
或者,将this
传递给另一个变量
var self = this;
session.onvalidatemerchant = function(event) {
self.(...)
}
https://stackoverflow.com/questions/68172067
复制