Introduction and use of seaJS

Table of contents

Introduction to seaJS

Quick and brief knowledge points

Use of seajs

use

Syntax: seajs.use(id, callback?)


Introduction to seaJS

What is seaJS?
  Similar to requireJS, seaJS is also a JS framework written in JavaScript. Its main function is to load JavaScript and other files according to different dependencies. It can be simply understood as a JS file loader, which is very suitable for browsers . Use, it can ensure that the current JS file is loaded after the dependent JS file is loaded. This can ensure the sequential loading order of each JS file in a project that uses a large number of JS files , ensuring that a certain file is avoided for some reason. Slow loading causes other fast-loading files to rely on some of its functions, and a function or variable cannot be found. This is very useful, and it is also the main value of seaJS (comply with CMD); but it is different from requireJS (comply with AMD specifications) differ.

Quick and brief knowledge points

 1. seajs.config({...}); //Used to configure Sea.js.
2. seajs.use(['a','b'], function(a,b){...}); //Used to load one or more modules in the page.
3. define(function(require, exports, module){...}); //Used to define modules. Sea.js respects one module and one file, and follows a unified writing method:
4. require(function(require){var a = require("xModule"); ... }); //require is used to obtain the interface of the specified module.
5. require.async, //Used to asynchronously load one or more modules inside the module. For example:

define(function(require){
    require.async(['aModule','bModule'],function(a,b){  // 异步加载多个模块,在加载完成时,执行回调
    a.func();
    b.func();
    })    
});

6. exports, //Used to provide external interfaces inside the module. For example:

define(function(require, exports){
    exports.varName01 = 'varValue';  // 对外提供 varName01 属性    
    exports.funName01 = function(p1,p2){  // 对外提供 funName01 方法
    ....
    }       
});

7. module.exports, similar to exports, is used to provide external interfaces inside the module. For example:

define(function(require, exports, module) {  
  module.exports = {  // 对外提供接口
    name: 'a',
    doSomething: function() {...};
  };
});

Use of seajs

use

seajs.use is used to load modules in the page. Through the use method, any module can be loaded in the page.

<script src="path/to/sea.js"></script>
<script>
seajs.use('./main');
</script>

Syntax: seajs.use(id, callback?)

// Load the module main, and when the loading is complete, execute the specified callback

seajs.use('./main', function(main) {
main.init();
});

The use method can also load multiple modules at once:

// Load module a and module b concurrently, and execute the specified callback when both are loaded

seajs.use(['./a', './b'], function(a, b) {
a.init();
b.init();
});

The callback parameter is optional. When only one module is loaded and no callback is needed, it can be simplified with the data-main attribute:

<script src=”path/to/sea.js” data-main=”./main”></script>

Equivalent to

<script src="path/to/sea.js"></script>
<script>
seajs.use('./main');
</script>

Guess you like

Origin blog.csdn.net/weixin_55953988/article/details/123207815