A Brief Introduction to RequireJS is the method of use. A brief introduction to requirejs
Introduction to RequireJS
RequireJS is a JavaScript module loader. It is ideal for browsers. Using RequireJS to load modular scripts increases the speed and quality of code loading.
Compatibility
Browser) |
Compatible or not |
IE 6 + |
Compatible✔ |
Firefox 2 + |
Compatible✔ |
Safari 3.2 + |
Compatible✔ |
Chrome 3 + |
Compatible✔ |
Opera 10 + |
Compatible✔ |
Advantages
Asynchronous loading of js files to prevent webpage response loss
Manage dependencies between modules to facilitate code compilation and Maintenance
Quick Start
Step 1
Introduce require. js
Dependencies in require () are an array. Even if there is only one dependency, you must use arrays to define
The second parameter is the callback function, which can be used to solve the dependencies between modules.
<!DOCTYPE html>
Step 2
Require. config is used to configure the module loading location
If the fixed position is relatively long, you can use baseUrl: "js", then you do not need to write js in paths.
<!DOCTYPE html>
Step 3
The require. config configuration is repeated in step 2. If the configuration is added to each page, it is not very good. requirejs provides a feature called "primary data ".
Create a main. js file and put require. config in step 2 into main. js.
<script data-main="js/main" src="js/require.js"></script>
Step 4
The modules loaded through require generally need to comply with AMD specifications, that is, define should be used to declare the module. However, in some cases, js with non-AMD specifications needs to be loaded. At this time, another function is required: shim
require.config({ shim: { "underscore" : { exports : "_"; }, "jquery.form" : ["jquery"] }});require(["jquery", "jquery.form"], function($){ $(function(){ $("#form").ajaxSubmit({...}); })});
The above is a brief introduction to RequireJS, that is, all the content of the Use method. I hope you can support more help ~