为什么不;这需要js的简单设置工作

why doesn't this simple require.js setup work?

本文关键字:简单 设置 工作 js 为什么不      更新时间:2023-09-26

我正在尝试学习如何使用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);
  });
})();

***小型blue-mod.js

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

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

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

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

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

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