Basic knowledge of "Requirejs" Requirejs

Source: Internet
Author: User

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

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.