简体   繁体   English

将脚本加载到TypeScript文件中?

[英]Load Script into TypeScript File?

I am attempting to add the following script tag to my Angular 2 project, however I am looking for a way to load this into the typescript file so that I can call its methods within the ts file. 我正在尝试将以下脚本标记添加到我的Angular 2项目中,但是我正在寻找一种方法将其加载到typescript文件中,以便我可以在ts文件中调用其方法。

<script type="text/javascript" src="https://js.squareup.com/v2/paymentform"></script>

There are a few things from the script that I need to be able to call in my typescript file, which are 脚本中有一些我需要能够在我的打字稿文件中调用的东西

let paymentForm = new SqPaymentForm();

and

paymentForm.requestCardNonce();

I am trying to impletement from SquareUp payments and they have an example here: https://docs.connect.squareup.com/articles/adding-payment-form 我正在尝试从SquareUp支付中实施,他们在这里有一个例子: https ://docs.connect.squareup.com/articles/adding-payment-form

However, this example doesn't translate well into the typescript setup that I am using. 但是,此示例不能很好地转换为我正在使用的打字稿设置。 Thanks for the help! 谢谢您的帮助!

You need to include the script in the index.html and import it into the component, like so. 您需要在index.html中包含该脚本并将其导入到组件中,就像这样。

declare var SqPaymentForm:any;
import "https://js.squareup.com/v2/paymentform";

The declare is required for it to properly compile, otherwise it doesn't know how to wait to link until it is JS. 声明是正确编译所必需的,否则它不知道如何等待链接直到它是JS。

Note: I have only done this with files that I downloaded, as opposed to access from the web, so if it complains about the import, you may need to download the JS and try that. 注意:我只对我下载的文件执行此操作,而不是从Web访问,因此如果它抱怨导入,您可能需要下载JS并尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM