【问题标题】:Using the requirejs shimConfig to load jQuery plugin使用 requirejs shimConfig 加载 jQuery 插件
【发布时间】:2014-08-06 00:36:59
【问题描述】:

此代码使用requirejs.shimConfig 加载jQuery.mCustomScrollbar 插件:

requirejs.config({
  baseUrl:'scripts',
  paths:{
    async:'lib/plugins/async',
    domReady:'lib/plugins/domReady',
    jquery:"http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min", 
    "jquery.mCustomScrollbar":"lib/plugins/jquery.mCustomScrollbar.concat.min"  
  },
  shim:{
        "jquery.mCustomScrollbar":{
           deps:['jquery'],
           exports:'jQuery.fn.mCustomScrollbar'
          }
       }
});

但是 Chrome 控制台显示 requirejs 尝试从 baseUrl 加载文件:

 GET http://localhost:8180/GoogleMapErpProject/scripts/jQuery.mCustomScrollbar.js 404   (Not Found) require.js:34
 Uncaught Error: Script error for: jQuery.mCustomScrollbar
 http://requirejs.org/docs/errors.html#scripterror 

编辑:

我找到了一个不令人满意的解决方案:

  requirejs.config({
baseUrl:'scripts',
paths:{
    async:'lib/plugins/async',
    domReady:'lib/plugins/domReady',
    jquery:"http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min",
    plugins:"lib/plugins"   
},
shim:{
    "jquery":{
        exports:"jQuery"
    },
    "plugins/jquery.mCustomScrollbar.concat.min":{
        deps:['jquery'],
        exports:'jQuery.fn.mCustomScrollbar'
    }
   }
});

当我在paths 中指定路径并在shimConfig 中使用该路径时,为什么它不起作用?

【问题讨论】:

    标签: javascript jquery requirejs shim


    【解决方案1】:

    来自 require.js 文档,

    baseUrl:用于所有模块查找的根路径。所以在上面 例如,“my/module”的脚本标签将有一个 src="/another/path/my/module.js"。加载时不使用baseUrl 纯 .js 文件(由以 a 开头的依赖项字符串表示 斜线、有协议或以 .js 结尾),这些字符串按原样使用, 所以 a.js 和 b.js 将从与 HTML 相同的目录加载 包含上述 sn-p 的页面。

    因此路径应该/可以是这样的:

    paths:{
        async:'lib/plugins/async',
        domReady:'lib/plugins/domReady',
        jquery:"http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min", 
        "jquery.mCustomScrollbar":"/lib/plugins/jquery.mCustomScrollbar.concat.min"  
      },
    

    paths:{
        ...
        "jquery.mCustomScrollbar":"./lib/plugins/jquery.mCustomScrollbar.concat.min"  
      },
    

    paths:{
        ... 
        "jquery.mCustomScrollbar":"lib/plugins/jquery.mCustomScrollbar.concat.min.js"  
      },
    

    如果您不希望它从 baseUrl 加载。

    【讨论】:

    • 我为jQuery.mCustomScrollbar 提供的值是相对于baseUrl 的。还要检查我发布的解决方案,这似乎有效,但如果我愿意,它不允许我做路径回退到。
    猜你喜欢
    • 2015-04-21
    • 2015-06-26
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    相关资源
    最近更新 更多