最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access JS global variable in Typescript file, using webpack - Stack Overflow

programmeradmin2浏览0评论

If I have a global variable var x in my main.js file, which i'd like to be accessible in a typescript file which is bundled by webpack into the same build/bundle.js, how would i go about this? I cannot access it directly as i assumed i might (them being in the same bundle at the end). I have read of export/import/require but cannot find how to apply this in the context of a webpack bundle.

If I have a global variable var x in my main.js file, which i'd like to be accessible in a typescript file which is bundled by webpack into the same build/bundle.js, how would i go about this? I cannot access it directly as i assumed i might (them being in the same bundle at the end). I have read of export/import/require but cannot find how to apply this in the context of a webpack bundle.

Share Improve this question asked Jul 12, 2017 at 11:23 rld001rld001 1531 gold badge3 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You may need to declare the variable in your typescript file so the piler knows its exists:

declare var x: any;

Adapted from this answer

ProvidePlugin from webpack should do the job, they have a pretty good example here

发布评论

评论列表(0)

  1. 暂无评论