我正在使用 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 屏幕截图:
最佳答案
在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/