zoukankan      html  css  js  c++  java
  • RequireJS 是一个JavaScript模块加载器

    RequireJS 是一个JavaScript模块加载器。它非常适合在浏览器中使用, 它非常适合在浏览器中使用,但它也可以用在其他脚本环境, 就像 Rhino and Node. 使用RequireJS加载模块化脚本将提高代码的加载速度和质量。

    IE 6+ .......... 兼容 ✔
    Firefox 2+ ..... 兼容 ✔
    Safari 3.2+ .... 兼容 ✔
    Chrome 3+ ...... 兼容 ✔
    Opera 10+ ...... 兼容 ✔

    获取 REQUIREJS§ 1

    去 下载 页面下载文件。

    添加 REQUIREJS§ 2

    注意: 关于 jQuery 集成的意见, 请看 jQuery 集成页面

    假定你的项目中 JavaScript 都放在一个 "scripts" 目录。 例如, 你的项目中有一个 project.html 页面和一些 scripts, 目录布局如下:

    • 项目目录/
      • project.html
      • scripts/
        • main.js
        • helper/
          • util.js

    添加 require.js 到 scripts 目录, 如下:

    • 项目目录/
      • project.html
      • scripts/
        • main.js
        • require.js
        • helper/
          • util.js

    为了充分利用的优化工具,建议您将所有的scripts放到的HTML外面, 然后只引用 require.js 来请求加载你其它的scripts:

    <!DOCTYPE html>
    <html>
        <head>
            <title>My Sample Project</title>
            <!-- data-main attribute tells require.js to load
                 scripts/main.js after require.js loads. -->
            <script data-main="scripts/main" src="scripts/require.js"></script>
        </head>
        <body>
            <h1>My Sample Project</h1>
        </body>
    </html>
    

    在 main.js, 你可以使用 require() 来加载所有你需要运行的scripts. 这可以确保你所有的scripts都是在这里加载的, 你可以 指定 data-main script 使用异步加载.

    require(["helper/util"], function(util) {
        //This function is called when scripts/helper/util.js is loaded.
        //If util.js calls define(), then this function is not fired until
        //util's dependencies have loaded, and the util argument will hold
        //the module value for "helper/util".
    });
    

    加载 helper/util.js 脚本. 想要充分利用 RequireJS, 请看 API 文档 去了解更多相关定义和模块的使用

    优化§ 3

    如果你最终决定在你在代码中使用, 可以使用 优化 结合 JavaScript 文件来减少加载时间. 在上面的例子中, 你可以结合 main.js 和 helper/util.js 加到一个文件中.

  • 相关阅读:
    【2021-03-03】人生十三信条
    【2021-03-02】勤奋和努力是积极乐观的自然导向
    【2021-03-01】解铃还需系铃人
    【2021-02-28】人生十三信条
    【2021-02-27】人生十三信条
    【2021-02-26】人生十三信条
    【2021-02-25】“活到老,做到老”的观念趋势
    【一句日历】2021年3月
    【2021-02-24】理想化中的积极进取精神
    机器人走方格
  • 原文地址:https://www.cnblogs.com/Anderson-An/p/9939319.html
Copyright © 2011-2022 走看看