RequireJS API
可以找到许多的解读,但是原文总是最重要的,也是最正宗的说明,直接访问 RequireJS 有时不太方便,这里将 RequireJS 2.0 API 的原文转载到博客园,方便查看。
This is the RequireJS 2.0 API. If you want 1.0: Link to 1.0.
§§ 1-1.3
§ 1.1
§ 1.2
§ 1.3
§ 1.3.1
§ 1.3.2
§ 1.3.3
§ 1.3.4
§ 1.3.5
§ 1.3.6
§ 1.3.7
§ 1.3.8
§ 1.3.9
§ 1.3.10
§§ 2
§§ 3
§§ 4-4.6
§ 4.1
§ 4.2
§ 4.3
§ 4.4
§ 4.5
§ 4.6
§ 4.7
§§ 5-5.4
§ 5.1
§ 5.2
§ 5.3
1 用法 § 1 1.1 加载 JavaScript 文件 § 1.1
RequireJS takes a different approach to script loading than traditional <script> tags. While it can also run fast and optimize well, the primary goal is to encourage modular code. As part of that, it encourages using module IDs instead of URLs for script tags.
RequireJS loads all code relative to a . The baseUrl is normally set to the same directory as the script used in a data-main attribute for the top level script to load for a page. The is a special attribute that require.js will check to start script loading. This example will end up with a baseUrl of scripts:
<!--This sets the baseUrl to the "scripts" directory, and loads a script that will have a module ID of ‘main‘--> <script data-main="scripts/main.js" src="http://www.mamicode.com/scripts/require.js"></script>
Or, baseUrl can be set manually via the . If there is no explicit config and data-main is not used, then the default baseUrl is the directory that contains the HTML page running RequireJS.
RequireJS also assumes by default that all dependencies are scripts, so it does not expect to see a trailing ".js" suffix on module IDs. RequireJS will automatically add it when translating the module ID to a path. With the , you can set up locations of a group of scripts. All of these capabilities allow you to use smaller strings for scripts as compared to traditional <script> tags.
RequireJS 假定所有的依赖都是脚本,所以不希望在模块 Id 的尾部使用 .js 后缀。在将模块 Id 转换为路径的时候,RequireJS 会自动添加。
There may be times when you do want to reference a script directly and not conform to the "baseUrl + paths" rules for finding it. If a module ID has one of the following characteristics, the ID will not be passed through the "baseUrl + paths" configuration, and just be treated like a regular URL that is relative to the document:
Ends in ".js".
Starts with a "http://www.mamicode.com/".
Contains an URL protocol, like "http:" or "https:".
In general though, it is best to use the baseUrl and "paths" config to set paths for module IDs. By doing so, it gives you more flexibility in renaming and configuring the paths to different locations for optimization builds.
Similarly, to avoid a bunch of configuration, it is best to avoid deep folder hierarchies for scripts, and instead either keep all the scripts in baseUrl, or if you want to separate your library/vendor-supplied code from your app code, use a directory layout like this:
www/
index.html
js/
app/
sub.js
lib/
jquery.js
canvas.js
app.js
require.js
in index.html:
<script data-main="js/app.js" src="js/require.js"></script>
and in app.js:
requirejs.config({ //By default load any module IDs from js/lib baseUrl: ‘js/lib‘, //except, if the module ID starts with "app", //load it from the js/app directory. paths //config is relative to the baseUrl, and //never includes a ".js" extension since //the paths config could be for a directory. paths: { app: ‘../app‘ } }); // Start the main app logic. requirejs([‘jquery‘, ‘canvas‘, ‘app/sub‘], function ($, canvas, sub) { //jQuery, canvas and the app/sub module are all //loaded and can be used here now. });
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/67533.html