为什么这个简单的require.js设置不起作用?

时间:2014-09-15 18:20:46

标签: javascript requirejs

我正在尝试学习如何使用require.js加载我的脚本,但我的设置/理解有问题。我看不出有什么问题。使用我缺少的设置非常简单。

当我在Chrome中加载此index.html页面时,没有任何脚本require.js操作正在运行。

*** index.html的

<html>
<head>
   <title>My Sample Project</title>
</head>
<body>
   <h1 class="h1">sample project header</h1>
   <script data-main="main" src="require.js"></script>
</body>
</html>

*** main.js

(function() {
  requirejs.config({
    //By default load any module IDs from baseUrl
    baseUrl: '',
    // paths config is relative to the baseUrl, and
    // never includes a ".js" extension 
    paths: {
      'small-blue-mod': './a-script'
    }
  });

  // Start the main app logic.
  requirejs(['small-blue-mod'], function (sbm) {
    alert(sbm.color);
  });
})();

***小蓝mod.js

define({
   color: "blue",
   size: "small" 
});

*文件系统看起来像......

index.html
main.js
require.js
FOLDER called "a-script"

a-script文件夹包含small-blue-mod.js

1 个答案:

答案 0 :(得分:1)

路径small-blue-mod是指a-script文件夹,要么像small-blue-mod/small-blue-mod那样需要,要么建议更改路径:

paths: {
  'small-blue-mod': './a-script/small-blue-mod'
}