SeaJS 按需加载js模块

234 阅读1分钟

1.目录结构

2.index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>seajs</title>
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
</head>
<body>
<input type="button" id="button1" value="Click">
<input type="button" id="button2" value="Click">
<script src="https://cdn.bootcss.com/seajs/3.0.2/runtime-debug.js"></script></body>
<script>
    seajs.use('./js/index.js');
</script>
</html>


3.index.js

define(function(require, exports, module) {
    $('#button1').click(function () {
        alert('button1');
    });

    require.async('./myModule', function(myModule) {
        myModule.myFun();
    });
});


4.myModule.js

define(function(require, exports) {
    var myModule = {
        myFun: function () {
            $('#button2').click(function () {
                alert('button2');
            });
        }
    };

    return myModule;
});


5.参考链接: www.zhangxinxu.com/sp/seajs/do…\