Global Variable with Typescript

Asked

Viewed 189 times

0

So... I’m making a code in Typescript in which I need to use a variable from another script, I’ll make it clear below:

<script type="text/javascript"> var global = "Variável Global";</script>
<script type="text/javascript" src="~/js/file.js"></script>

And inside the file.js I need to capture this variable, someone help me ?

Example:

file js.:

window.onload = function () {
     alert(global);
}
  • (window as any).global works?

  • Where should I put this ? @Luizfelipe

  • By accessing the global variable. By the way, edit your question by posting the code where you try to access the global variable so I have a little more context.

  • Then it would be so alert(window!.global);? @user140828

  • @Luizfelipe, the code is basically that right there, when the page loads, it gives the variable Alert

  • 1

    You tried to alert((window as any).variavelGlobalAqui)?

  • You can use declare var global: tipo.

  • It worked @Luizfelipe, thank you very much... could answer the question for people who have the same question in the future?

Show 3 more comments

1 answer

0


Simply put, you can access global variables like this:

const globalVariable = (window as any).globalVariable

For example:

alert((window as any).globalVariable)
  • It worked as expected, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.