使用ApplePay JS API的时候,必须在用户点击支付按钮后才能调用它。这是由于安全原因所导致的。如果我们在Angular的模板中使用ngIf指令,可能会导致屏幕无法及时更新。以下是一种解决方法,使用一个布尔变量来控制ngIf指令:
component.ts:
public showApplePayButton: boolean = false;
public onPaymentButtonClicked(): void {
this.showApplePayButton = true;
this.applePayService.initiatePayment();
}
public onPaymentComplete(): void {
this.showApplePayButton = false;
}
component.html:
在这个示例中,当用户点击“Pay with Apple Pay”按钮时,我们设置showApplePayButton变量为true,这将导致ngIf指令将Apple Pay按钮显示到屏幕上。当用户完成支付时,我们将showApplePayButton变量设置为false,这将导致ngIf指令将该按钮从屏幕上删除。
请注意,因为Apple Pay按钮是在模板中渲染的,所以我们必须在component.ts文件中导入并注入一个ApplePayService。此外,我们还必须在index.html文件中引入Apple Pay的JavaScript SDK。
...
...