简体   繁体   English

如何使用RequireJS加载Underscore库?

[英]How to load Underscore library with RequireJS?

require(['underscore'], function ($, _) {
  ...
});

Doesnt work! 不起作用! ( _ is not a function) _不是函数)

How to manage it? 如何管理?

Note that underscore.js doesn't register itself as an AMD module (though it did for a brief time in earlier versions), thus it can't be used in a require() call without some configuration using "shim:" like so: 请注意,underscore.js不会将自己注册为AMD模块(虽然它在早期版本中只执行了很短的时间),因此如果没有使用“shim:”这样的配置就不能在require()调用中使用它:

require.config({
    paths: {
        jquery: 'lib/jquery.min',
        underscore: 'lib/underscore-min'
    }
    shim: {
        "underscore": {
            exports: "_"
        }
    }
});

See the docs at: http://requirejs.org/docs/api.html#config-shim 请参阅以下文档: http//requirejs.org/docs/api.html#config-shim

Before shim: was added to require.js, you could do something similar with the plugin use.js (in case you need to use an older version of require.js). shim:添加到require.js之前,你可以使用插件use.js做类似的事情(如果你需要使用旧版本的require.js)。

As of this writing, the current version of require.js is 2.1.8. 在撰写本文时,require.js的当前版本是2.1.8。

Alternatively, you can use lodash.js as a drop-in replacement for underscore.js - it does register itself as an AMD module, so you can use it with no extra config: http://lodash.com/ 或者,您可以使用lodash.js作为underscore.js的替代品 - 它确实将自己注册为AMD模块,因此您可以在没有额外配置的情况下使用它: http//lodash.com/

I think the problem is the order of args passed in to your callback. 我认为问题是args传递给你的回调的顺序。

Should be: 应该:

require(['underscore'], function (_, $) {
 ...
});

Also you need to be using underscore version 1.2.1 which added this functionality. 您还需要使用添加此功能的下划线版本1.2.1。

Here are the checkpoints for you to make sure what you need works 以下是检查点,以确保您需要的工作

  1. Get require-jquery.js and put it to your /js-root dir 获取require-jquery.js并将其放到/js-root目录中

  2. Add to your HTML, right before the closing </body> tag: <script data-main="/js-root/main-js-file-name" src="/js-root/require-jquery.js"></script> 在结束</body>标记之前添加到HTML: <script data-main="/js-root/main-js-file-name" src="/js-root/require-jquery.js"></script>

  3. Get underscore adapted for AMD , and put it to /js-root dir as well 获得适用于AMD的下划线 ,并将其放入/js-root目录

  4. In main-js-file-name.js main-js-file-name.js

write: 写:

require(["jquery", "underscore"], function ($, _) {
    ...
});

Similarly, in your non-main AMD JS files, when defining a module, to use _ , write: 同样,在非主要的AMD JS文件中,在定义模块时,要使用_ ,写:

define(["jquery", "underscore"], function ($, _) {
    ...
    return theModuleObjectOrFunction;
});
require(["underscore"], function() { 
  console.log(_ === window._);
});

it all depends where the script is based. 这一切都取决于脚本的基础。 since i don't see you specified a baseUrl, the baseUrl will be the default, that means, either 2 things: 因为我没有看到你指定了baseUrl,所以baseUrl将是默认值,这意味着两件事:

  1. your script is directly inside a html file, and in your case it will thus look for underscore.js in the same directory of the html file 你的脚本直接在一个html文件中,在你的情况下,它将在html文件的同一目录中查找underscore.js
  2. your script is in a javascript file referenced by your html file, it will now search for underscore.js in the directory of your custom javascript file. 你的脚本在你的html文件引用的javascript文件中,它现在将在你的自定义javascript文件的目录中搜索underscore.js。

check if the underscore.js is actually there. 检查underscore.js是否真的存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM