首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >离子4 Stripe.js集成

离子4 Stripe.js集成
EN

Stack Overflow用户
提问于 2020-03-25 18:45:44
回答 1查看 333关注 0票数 0

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

这很好,但是我不能将stripe.js结果存储到局部变量中。

首先是index.html的代码

代码语言:javascript
复制
<!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

代码语言:javascript
复制
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代码工作在finde,我正在获取数据,我可以在控制台中看到它。但我不能把它存储在变量obj中。我得到了错误: Uncaught (在承诺中):TypeError:无法设置属性'obj‘的未定义的TypeError:无法设置属性'obj’的未定义

正如我所想的,问题是,我不能将obj访问到条形函数中。有可能解决这个问题吗?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-25 18:52:30

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

代码语言:javascript
复制
this.stripe
    .confirmCardPayment('someIntentID')
    .then((result) =>
    {
      console.log(result.paymentIntent);
      this.obj = result.paymentIntent;
    });
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60855396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档