基本的require.js示例

时间:2013-09-02 21:27:08

标签: javascript html requirejs

的index.html

<html>

    <head>
        <script data-main="main" src="http://requirejs.org/docs/release/2.1.8/minified/require.js"></script>
    </head>

    <body></body>

</html>

main.js

requirejs.config({
    paths: {
        jquery: 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min'
    }
});

define(['jquery'], function ($) {
    console.log($);
});

为什么console.log给我未定义?

1 个答案:

答案 0 :(得分:2)

AMD support(RequireJS需要)has been added to jQuery 1.7,您正在尝试使用jQuery 1.6。

为了将jQuery 1.6与RequireJS一起使用,请尝试添加shim配置:

requirejs.config({
  paths: {
    jquery: 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min'
  },
  shim: {
    jquery: {exports: '$'}
  }
});

或者,你可以使用更新版本的jQuery(至少1.7):

requirejs.config({
  paths: {
    jquery: 'http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min'
  }
});