A Brief Introduction to RequireJS is the method of use. A brief introduction to requirejs

Source: Internet
Author: User

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 ~

Related Article

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.