我知道这已经被讨论过,但是经过一段时间的搜索后,我无法弄清楚为什么我的小设置不能使用 requireJS 正确加载 jquery。
我正在从“file://”运行一个小型示例 html 页面,并尝试通过调用 require 来加载 2 个模块:
如果我的自定义模块正确加载(我可以在 require 调用中使用它及其别名,jquery 始终未定义)
我尝试在 require.config 以及 shim (导出“$”或“jQuery”)中设置路径,但它不起作用。
我能让 jquery 正确加载的唯一方法是删除所有路径定义并在我的文件系统“jquery.js”上命名 jquery 文件
这是我的配置:
main.js:
require.config({
baseUrl: 'scripts',
paths: {
jquery: 'jquery-2.1.3.min' //does not work
//jquery: 'http://code.jquery.com/jquery-2.1.3.min.js' //does not work either
}
});
console.log( "main.js is loaded" ); //this is correctly ouputed to the console
测试.html:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<!--
... page content ...
-->
<!-- main.js is loaded from data-main -->
<script data-main="scripts/main" src="scripts/require.js"></script>
<script type='text/javascript'>
require(
['jquery','custom'],
function($, Custom){
console.info('$:');
console.info($); //outputs undefined
console.info('Custom:');
console.info(Custom); //outputs my custom object
});
</script>
</body>
</html>
再一次,如果我删除 jquery 的所有路径定义,并简单地将我的 jquery js 文件命名为“jquery.js”,它就可以工作,但这很草率。
有人可以指出我正确的道路吗?