How to wait when data-main is loaded with Require.JS before executing script?

和自甴很熟 提交于 2020-01-04 05:48:13

问题


I'm having the following code:

<script data-main="scripts/main" src="components/requirejs/require.js"></script>
<script src="scripts/example1.js"></script>

Inside example1 I'm doing a require for some components of which the path is supposed to be set in scripts/main. However, the path is not correctly set (judging by the console feedback) which leads me to believe that require.js hasn't finished loading files referenced in scripts/main.

This all is pretty logical (async and all that), but I'd like to know how to run code inside example1.js after all this is set-up.

Normally, one would simply add the app-init code to scripts/main probably and be done with it, but I'm building up some examples, which all share the same scripts/main file (which is pretty big) and I don't want to duplicate that effort.

So how to do this?


回答1:


Ended up doing the following, which isn't ideal (sets a global), but it's ok for my examples.

 <!-- sets window.requireconfig with an json object defining paths, etc.-->
 <script src="scripts/requireconfig.js"></script> 

 <!-- in top of example1.js do : require.config(window.requireconfig); -->
 <script data-main="scripts/example1" src="components/requirejs/require.js"></script>


来源:https://stackoverflow.com/questions/15386939/how-to-wait-when-data-main-is-loaded-with-require-js-before-executing-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!