Requirejs-在加载数据之前配置require-main [英] Requirejs - Configuring require before loading data-main

查看:315
本文介绍了Requirejs-在加载数据之前配置require-main的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们是第一次使用requirejs,在构建依赖项时遇到了麻烦.

我已经将我的主要app.js文件定义为index.html中的data-main属性:

<script data-main="src/app" src="/js/lib/require/require.js"></script>

但是,我有一个文件,该文件定义了我所有需要的路径/填充程序配置,并且我希望该文件在app.js文件之前运行.我需要运行它,以便可以在我的app.js中引用已配置的路径作为依赖项.

我认为正确的方法不是将config.js设置为数据主体. 我尝试将config.js设置为这样的依赖项:

   <script type="text/javascript">
        var require = {
            baseUrl: "/",
            deps: ["src/config"]
        }
    </script>
    <!-- data-main is the main js file of the app -->
    <script data-main="src/app" src="/js/lib/require/require.js"></script>

但这没有帮助.

有什么建议吗?

解决方案

对于我来说,我将config.js加载到app.js中以共享每个页面的配置.

例如:

 require(['config'], function(){
  require(['module','another'], function(){
    // run with all modules
  });
});
 

要优化此项目,使用has.js是减少HTTP连接的更好方法.有关更多详细信息,请参见此示例项目.. >

We're using requirejs for the first time, and I'm having trouble structuring my dependencies.

I've defined my main app.js file as the data-main attribute in my index.html:

<script data-main="src/app" src="/js/lib/require/require.js"></script>

But, I have a file which defines all my require path/shim configurations, and I want that to run before the app.js file. I need it to run so that I can reference configured paths as dependencies in my app.js.

I don't think the right way is to put my config.js as the data-main. I tried setting the config.js as a dependency like this:

   <script type="text/javascript">
        var require = {
            baseUrl: "/",
            deps: ["src/config"]
        }
    </script>
    <!-- data-main is the main js file of the app -->
    <script data-main="src/app" src="/js/lib/require/require.js"></script>

but that didn't help.

Any suggestions?

解决方案

In my case, I load config.js in app.js to share configuration for each pages.

For instance:

require(['config'], function(){
  require(['module','another'], function(){
    // run with all modules
  });
});

To optimize this project, using has.js is better way to reduce HTTP connection. For more detail, see this sample project.

这篇关于Requirejs-在加载数据之前配置require-main的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆