离子 4 Stripe.js 集成

Ionic 4 Stripe.js Integration

我已经通过 index.html.stripe.js 将 stripe.js 集成到我的离子项目中

这很好用,但我无法将 stripe.js 结果存储到局部变量中。

首先是index.html

的代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <title>Ionic App</title>

  <base href="/" />

  <meta name="viewport" content="viewport-fit=cover, width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  <meta name="format-detection" content="telephone=no" />
  <meta name="msapplication-tap-highlight" content="no" />

  <link rel="icon" type="image/png" href="assets/icon/favicon.png" />

  <script src="https://js.stripe.com/v3/" async></script>

  <!-- add to homescreen for ios -->
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
</head>

<body>
  <app-root></app-root>
</body>

</html>

和 home.page.ts

import { Component } from '@angular/core';
declare var Stripe;

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})


export class HomePage {

  stripe = Stripe('my_public_key', {stripeAccount: "connected_stripe_account_key"});
  obj:any;

  constructor() 
  {}

  doCheck()
  {
    this.stripe
    .confirmCardPayment('someIntentID')
    .then(function(result) 
    {
      console.log(result.paymentIntent);
      this.obj = result.paymentIntent;
    });
  }
}

Stripe 代码工作正常,我正在获取数据并且可以在控制台中看到它。但是我不能将它存储在变量 obj 中。我收到错误:Uncaught (in promise): TypeError: Cannot set 属性 'obj' of undefined 类型错误:无法设置未定义的 属性 'obj'

正如我所想,问题是我无法将 obj 访问到条带函数中。有没有可能解决这个问题?

谢谢!

您有 'this' 作用域问题,因为它没有指向匿名函数中的 Class。使用箭头函数来避免它:

this.stripe
    .confirmCardPayment('someIntentID')
    .then((result) =>
    {
      console.log(result.paymentIntent);
      this.obj = result.paymentIntent;
    });