Official website documents, look a bit foggy, personal writing documents and a variety of, well, I can only read through the various documents themselves, summed up. Refine's understanding.
First, concept, modular management
1. Replace the URL address with the module ID
2. Load all code relative to BaseURL's address
3, in the <script> tag has a special attribute data-main= "", Requirejs use Data-main to start the JS loading process, BaseURL is generally set to a directory consistent with the project properties.
<script tyoe="text/javascript" data-main="script/main.js" src="script/require.js"></script>//官方
(1), Data-main to the right of the main file can be without the. js suffix, you can also have a. js suffix A, demo1 file directory:
HTML file:
<!DOCTYPE html>
lang="en">
<meta charset="UTF-8">
<script src="require.js" data-main="main.js"></script>
<title></title>
<body>
</body>
Main.js file:
/**
* Created by s9-1102 on 2015/4/30.
*/
require.config({
paths:{
app:‘js/app‘
}
})
require([
‘app‘
])
App.js file:
define(function(){
alert(123);
})
In the above code, the Main.js file, with and without the. js suffix, allows the result to be the same. Official explanation: Require all resources assumed by default are JS files, so it is not necessary to add the. js suffix on the module ID (data-main). Requirejs automatically complements the suffix when parsing to path (paths) within the module ID (data-main).
4, Data-main entry point (1), Requirejs check Data-main property (2) When loading, can set module load option in Data-main, load module. The modules in the *data-main are loaded asynchronously. If other modules are loaded on the page, there is no guarantee that the JS files loaded in the page, and the dependencies of the modules in the Data-main, are, of course, dependent.
5, define the module (1), a good definition of a scope to avoid global namespace pollution. (2), explicitly listing dependencies (3), injecting dependency injection in the form of function arguments (4), allowing multiple modules to be loaded, the order of loading is variable, but the order of dependency is correct.
From for notes (Wiz)
Require study notes (i)