【问题标题】:Shim - how to load files that contains dotsShim - 如何加载包含点的文件
【发布时间】:2013-09-05 13:56:22
【问题描述】:

如主题。我的文件结构是:

js/ 
 |- bootstrap
 |   |- module1
 |   |- module2
 |
 |- jquery.min.js
 |- main.js

我的 main.js 文件是

requirejs.config({

    paths: {
        dropdowns : 'bootstrap/module1'
        ,fixes : 'bootstrap/module1'
    }
    ,shim: {
        'jquery.min' : ['jquery']
    }
});

requirejs(['jquery', 'dropdowns', 'fixes'], function ( $, Dropdowns, Fixes ) {

    console.log( $ );
    var fixes = new Fixes();

});

现在...它在控制台中抛出以下错误:

GET http://myurl/myapp/js/jquery.js 404 (Not Found) 

正如我们所见,它用 shim 松开了点。我的问题是“如何加载像 'jquery.min.js' 这样的文件 .js 部分之前的点在哪里?”

【问题讨论】:

    标签: javascript requirejs


    【解决方案1】:

    如果您只是想说“在请求 'jquery' 模块时,从 'jquery.min.js' 加载它”,那么您的配置应该如下所示:

    requirejs.config({

    paths: {
        dropdowns : 'bootstrap/module1'
        ,fixes : 'bootstrap/module1'
        ,jquery: 'jquery.min'
    }
    

    });

    如果您需要说明您的 其他 模块需要 jquery,则 shim 部分将发挥作用:

    shim: {
        dropdowns: {
            deps: ['jquery']
        },
        fixes_validate: {
            deps: ['jquery']
        }
    }
    

    有关shim的更多详细信息,另请参阅此答案:Requirejs why and when to use shim config

    【讨论】:

      【解决方案2】:

      这不是您加载名为 jquery.min 的文件的方式。这行是说 jquery.min 依赖 jquery,没有太大意义。

          'jquery.min' : ['jquery']
      

      由于 jQuery 定义了一个 AMD 模块,你应该尝试这个来指定路径

      requirejs.config({
      
          paths: {
              dropdowns : 'bootstrap/module1'
              ,fixes : 'bootstrap/module1'
              ,jquery: 'jquery.min'
         }
      });
      

      这可能会给出更清晰的解释http://requirejs.org/docs/jquery.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-17
        • 1970-01-01
        • 2018-04-09
        • 2014-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-22
        相关资源
        最近更新 更多