Mir*_*lov 5 javascript requirejs underscore.js
我试图同时使用下划线和Underscore.string与RequireJS.
内容main.js:
require.config({
paths: {
'underscore': '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min',
'underscore-string': '//cdnjs.cloudflare.com/ajax/libs/underscore.string/2.3.0/underscore.string.min',
},
shim: {
'underscore': {
exports: '_'
},
'underscore-string': {
deps: ['underscore'],
exports: '_s'
},
}
});
var modules = ['underscore-string'];
require(modules, function() {
// --
});
Run Code Online (Sandbox Code Playgroud)
浏览器看到了_,但没有看到_s- 它是未定义的.
理想情况下,我想有下划线下_和Underscore.string下_.str,但_并_s是没关系.我怎样才能做到这一点?
版本: RequireJS 2.1.5,Underscore 1.4.4,Underscore.string 2.3.0
注意:感谢@jgillich确保,这些路径有两个斜杠(//cdnjs.cloudfare.com/...),否则浏览器会认为URL是相对于服务器的,而Firebug会抛出:
Error: Script error
http://requirejs.org/docs/errors.html#scripterror
Run Code Online (Sandbox Code Playgroud)
Mir*_*lov 10
我发现了错误.出于某种原因,RequireJS不能与cdnjs.com的Underscore.string版本一起使用,所以我用Github版本替换它.我想这与提交9df4736有关.
目前我的代码如下所示:
require.config({
paths: {
'underscore': '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min',
'underscore-string': '//raw.github.com/epeli/underscore.string/master/dist/underscore.string.min',
},
shim: {
'underscore': {
exports: '_'
},
'underscore-string': {
deps: ['underscore'],
},
}
});
var modules = ['underscore', 'underscore-string'];
require(modules, function(_) {
// --
});
Run Code Online (Sandbox Code Playgroud)
Underscore.string驻留在_.str.
编辑:截至2013年7月16日,CDNJS版本随上游更新.