javascript - 使用 Angular2 和 stripe.js 导入 Stripe

标签 javascript ionic-framework angular stripe-payments ionic2

我正在使用 Angular2、Ionic2 和 Stripe.js 进行支付处理。此线程在这里 ionic2 with stripe payment gateway正在使用插件 https://github.com/Telerik-Verified-Plugins/Stripe/blob/master/doc/index.md但这并不安全,因为您必须将 key 嵌入到应用程序中。甚至页面都在告诉人们不要使用这个插件。

我在这里尝试使用 node.js 版本:

https://www.npmjs.com/package/stripe

但是,我不知道如何执行 var stripe = require('stripe')(' your stripe API key ');在 TypeScript 中,你必须使用 import .

最后,我确实放了<script type="text/javascript" src="https://js.stripe.com/v2/"></script>在 index.html 和 stripe 中变量在每个组件内全局显示。但是,我觉得这不是正确的方法,因为 stripe当我在每个组件或页面中使用它时,对象可能还没有准备好。

使用 Angular2 和 Stripe.js 的正确方法是什么? Ionic2 特别好,但可选。

谢谢

更新 1

我试过了 npm install stripe然后使用 import '../../node_modules/stripe/lib/stripe.js';但仍然有错误:

TypeScript error: /Users/username/Documents/StripePayment/app/pages/home/home.ts(16,23): Error TS2304: Cannot find name 'Stripe'.
Error: Cannot find module '../../node_modules/stripe/lib/stripe.js' from '/Users/username/Documents/StripePayment/app/pages/home'

这是我的带有目录结构的 VS Code 屏幕截图:

enter image description here

最佳答案

在index.html中添加scripttag,然后在home.ts中的imports之后声明

声明 var Stripe: any; 我相信这是在 ng2 中导入外部库的正确方法

来源:Nic Raboy 那里有更多信息;安装外部库的更好方法是从 DefinitelyTyped 下载 typedef。并使用 $ typings install 安装 然后你应该可以像往常一样导入

当然,前提是 DefinitelyTyped 存储库中有 typedef。 不过,Stripe 库似乎不存在 typedef。

关于javascript - 使用 Angular2 和 stripe.js 导入 Stripe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36543747/

相关文章:

javascript - 将当前 iframe 内容导出为 HTML 文件

reactjs - 你可以将 Redux 与 Ionic/React 一起使用吗?

cordova - Ionic - 安装推送插件出现错误

angular - 'This' 在 subscribe 中未定义

css - 在单个 NgStyle 标签中混合多个条件?

javascript - 如何从swift调用javascript

Javascript比较多维数组中的偶数和奇数

javascript - 解析以 "var ..."开头的 JSON 时出错

javascript - 无法在 ionic 模板中显示成功的 angularjs $http 从本地服务器获取响应

angular - 使用 angular 构建多语言和多方向网站的最佳方法是什么?