javascript - Ionic 4 Stripe.js 集成

标签 javascript angular ionic-framework stripe-payments ionic4

我已通过 index.html 将 stripe.js 集成到我的 ionic 项目中。

这工作正常,但我无法将 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 中。我收到错误:未捕获( promise 中):TypeError:无法设置未定义的属性“obj” 类型错误:无法设置未定义的属性“obj”

正如我认为的,问题是,我无法将 obj 访问到 strip 函数中。有可能解决这个问题吗?

谢谢!

最佳答案

您遇到“this”范围问题,因为它没有指向匿名函数中的类。使用箭头功能来避免它:

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

关于javascript - Ionic 4 Stripe.js 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60855396/

相关文章:

javascript查找以字符串作为id的元素

javascript - Node : How to free buffers that get allocated outside of the V8 memory heap

angular - Azure Web App wwwroot 在发布时重复

css - Angular Material 对话框面板类不会更新

javascript - 如何创建一个左、中、右各有图标的列表项?

javascript - REST 方法的 URL

Angular2 - 添加新的 <li> 项目 onClick 事件

angularjs - 将 Visionmedia/debug 添加到 Ionic/AngularJS 应用程序

javascript - 更改状态时某些参数不会传递值

javascript - GetElementsByTagName(标签).length