I would like to use a <script> in my Angular component, how do I do that ? (TypeScript, Angular12)

Viewed 102

This is the code that I would like to use in my Angular component:

  <form action="https://www.meu-site.com/processar-pagamento" method="POST"> <script
src="https://www.mercadopago.com.br/integrations/v1/web-tokenize-checkout.js"
data-public-key="ENV_PUBLIC_KEY"
data-transaction-amount="100.00"></script></form>
1 Answers

If you have to load the script from remote site you can add it to your index.html file, like you would do in a regular html site.

index.html

<!-- get script from 3rd party site -->


<script
     src="https://www.mercadopago.com.br/integrations/v1/web-tokenize-checkout.js"
     data-public-key="ENV_PUBLIC_KEY"
     data-transaction-amount="100.00">
</script>

Then, to use the the script in your component, you need to manually declare the exported variables / functions to avoid TS compilation errors

** formComponent.ts**

declare let web-tokenize-checkout: any; //declare the name of the global object you got from the script you added

//use your script functionality, That part is really depends on how the script you imported works
web-tokenize-checkout.doWhatEverYouNeed(....);
Related