1. Advantages of Requirejs
1) asynchronous loading of dependent files
2) Manage File load order
3) Managing the file Load package path
2. Requirejs Download location
Https://github.com/jrburke/requirejs
3. Requirejs Demo
The dependent files are as follows:
1) index.html homepage, introducing Requirejs, specifying the entire page JS execution portal
2) main.js page Execution portal, define JS BaseURL and public package
3) Lib.js Main.js dependent module
Index.html
<!DOCTYPE HTML> <HTML><Head> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /> <title>Reporting Platform</title></Head><Body></Body><Scriptsrc= "/resources/um_report/js/require.js"Defer Async= "true"Data-main= '/resources/um_report/js/main.js '></Script></HTML>
Main.js
Requirejs.config ({ baseUrl: '/resources/js/'}) require ([' Lib '],function (Lib) { console.log (' Lib '); Lib.say (' hello '); Console.log (Lib.color);});
Lib.js
Define (function () { var say1 = function (msg) { console.log (msg); } return { say:say1, color: ' Red ' };});
The process is as follows:
1) index.html load require.js Get entry Data-main definition of main function
2) main.js defines the base path of all JS, followed by the JS are in the base path of the relative path,
It also introduces a module called Lib under the base path, executes the Lib's say method, and prints the Lib.color
3) Lib.js defines an object that returns the Say method and the Color object
Basic knowledge of "Requirejs" Requirejs