1, first download the Seajs, and then in our project to create a new folder named Sea (the name can be arbitrarily), the download of the Sea.js file to the inside, and finally in the HTML document with the script tag introduced can be used.
2, in the modular, a JS file is a module, JS folder All JS files are modules. Of course, the ordinary JS file into a module, to do a change. Use the Define () method to define the JS file as a module. The define method takes a function as a parameter , and the function has three parameters, as follows.
Define (function(require,exports,module) {// require introduces the dependency of the module;// Exports exposes the method of the module, provides an external interface;//Module means the modules themselves; Note that these two parameter names cannot be changed, and the order of the parameters cannot change. function Show () { alert (1); } = Show;})
3, how to call the module in the HTML document, with the Seajs.use method, it accepts two parameters, one is to introduce the module, and one is to introduce a successful callback function. The callback function has a parameter that points to the exports in the module that we introduced, so you can use this parameter to expose the method to the module.
<script> seajs.use (function(ex) { // Note file path,./ Represents the folder in which the current HTML document resides ex.show (); // The ex parameter points to the exports in the module })</script>
4, if a module relies on another module, it is necessary to use the Require parameter, which takes a parameter, the path of the introduced module, the return value is also pointed to the introduction of the module exports, so can be called by the return value of the module exposed methods. Change the module above 2 to introduce dependencies.
Define (function(require,exports,module) {// require introduces the dependency of the module;// Exports exposes the method of the module, provides an external interface;//Module means the modules themselves; Note that these two parameter names cannot be changed, and the order of the parameters cannot change.
var external = Reqiure ("./moudules3.js"), var external = reqiure ("./moudules3.js") Check the wrong for a long time.
function Show () { alert (external.a);
= Show;})
Modular development Seajs