简体   繁体   中英

How do a define a global variable in the ember-cli environment?

I'm in the process of porting my ember app to ember-cli. I have top level function I'm using that's define like var fooBar = function(a) { }; .

I moved that to app/utils/foo-bar.js (it was all in js/app.js), and added export default fooBar; to the end of the file.

I think that would be fine if the place I was using it could do an import, but for reasons I don't want to get into, I need defined as a global variable.

But I still want it in app/utils/ and to be able to import it, as I might rewrite the thing causing me to need it to be a global someday.

Is there some way to maybe have a <script> tag in my index.html that looks up the module and assigns it to a global variable?

Edit:

My forBar function that I want to be global needs to reference another variable via an import statement. But in /vendor I can't use define or import .

So just throwing it in /vendor just gives me back the same problem but for a different variable.

A hacky workaround is to put it in a file in the /vendor folder and then app.import that file. That will add it to the vendor.js file making the global available in the browser context.

Ok, I think I found the answer.

<script type="text/javascript">
    var fooBar = require(['path/to/foo-bar']).default;
</script>

What I didn't realize is ember-cli is using requirejs, once I figured that out, I realized this is actually a requirejs question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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