Requirejs Basic Use

Source: Internet
Author: User

In Requirejs, the suffix of. js can be omitted from writing.
<script data-main="main" src="require.js"></ Script>
After the require.js is loaded, the value of the Data-main property of the script tag on the page is found, then loaded, data-main the specified JS file


Find the JS file named Main
JS1, Js2, JS3, and more JS files can also be loaded at the same time via Requirejs
The 2nd callback function will not be processed until all the imported JS files have been loaded.

Require (['js1'js2'js3') , function () {Console.log ('js1 js2 js3 loaded')var total = num1 + num2 + num3console.log (total) hello1 () Hello2 () Hello3 ()})


The use of Requirejs can easily import JS files, but pay attention to the JS file in the variable name and the method name and other JS files in the variable name Method name conflict problem
Causes of conflict:
Browser-side JS file There is a common global scope, the global scope of the variable name, the method name may be overwritten with the Requirejs load JS file, will automatically add a script tag in the head tag, these script tags with the async attribute, The Async property causes the browser to asynchronously load these JS files in parallel (that is, start loading the JS files at the same time)

Create a module

Determine if require is correct

if (typeof"function"&&define.amd) {console.log ("  Define from Require.js"

Requirejs = = require

Requirejs (["js4"],function (js4) {console.log (" exported is:"  ) Console.log (JS4);});

Modular Nodejs (server side, CommonJS) require.js (client, AMD) Sea.js (China, client, CMD) ES6 (ECMA)

Defining modules

Parameter a module name must be a string. Parameter 2 dependent,

Define (["require","exports","Module","JS5"],function (Require, exports, MODULE,JS5) {//exports = "heavenly"; failureModule.exports ="Days of seclusion"; Console.log (require); Console.log (exports); Console.log (module) ;//var num = require ("JS5") gets JS5//console.log (num);});

Or so write define (function (require, exports, MODULE,JS5) { (PS: recommend the above kind.) This kind of writing consumption can be relatively large)

4 Ways to Export
Exports is able to use. Add property, cannot use = re-assign value
Module.exports can be used either. Add an attribute or use = to re-assign a value
Return the most common method, equivalent to use in Module.exports =
Define (object) is directly followed by an object, equivalent to Module.exports = {};
require = = Requirejs () in the global;
function scope require = = Localrequire (); The return value is the export of the module

Requirejs Basic Use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.