I have two files that both need a global variable. I have a click button. When it\'s clicked, runs a function. The code looks like this:
file1:
var g
If you truly want a global variable (not advisable, of course) then you're always 100% free to do
window.globalVar = 0;
in any of your modules.
The more robust solution would of course be to have this global variable sit in some sort of dedicated module, like
globalVar.js
export default {
value: 0
};
and then you could
import globalVal from './globalVar';
globalVal.value = 'whatever';
from any modules needed.
The only risk would be that webpack might duplicate this same "global" value into multiple bundles if you're code splitting, depending on your setup. So separate module would be using their own local copy of this not-so-global variable. EDIT - this isn't true. webpack never did this; that comment was based on a misunderstanding on my part.