Home > Web Front-end > JS Tutorial > Require.js detailed explanation

Require.js detailed explanation

php中世界最好的语言
Release: 2018-03-14 09:15:08
Original
1625 people have browsed it

This time I will bring you a detailed explanation of Require.js. What are the precautions for using Require.js? The following is a practical case, let's take a look.

index.html

<!DOCTYPE html><html>
    <head>
    </head>
    <body>
        <span>body</span>
    </body>
    //将require引入,放在html页面最下方,或设置异步请求
    //即: <script src="js/lib/require.js" defer async="true"></script>
      <script src="js/require.js" data-main="js/main"></script></html>
Copy after login

main.js

require.config({    //配置路径
    paths: {        "jquery": ["http://libs.baidu.com/jquery/2.0.3/jquery", "jquery.min.js"],        "a": "a"
    }
})require(["jquery", "a"], function($,a) {
    $(function() {              //调用即可,此处的a并无意义,仅为指示,与引入文件顺序有关,即若
              //function($,a)中a换了一种称谓,这里调用时保持一致即可
        a.fun1();
        alert("load finished");
    })
})
Copy after login

a.js

function fun1(){
   alert("it works");
 } //切记,如果需要在调用该函数,一定要记得返回,个人理解为暴露接口return {     fun1: fun1
 };
})```
Copy after login

I believe you have mastered the method after reading the case in this article, and there will be more exciting things. Please pay attention to other related articles on php Chinese website!

Recommended reading:

How to implement node connection to mysql

Use JS to implement sorting algorithm

The above is the detailed content of Require.js detailed explanation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template