With the implementation of module definition specifications such as define, we can develop many modules. But just having a bunch of modules doesn’t work, we have to make them run. In SeaJS, it is very simple to start the module system:
<script src="path/to/sea.js"></script> <script> seajs.use('./main'); </script>
seajs.use is used to load modules in the page. Through the use method, any module can be loaded in the page.
Syntax: seajs.use seajs.use(id, callback?)
// 加载模块 main,并在加载完成时,执行指定回调 seajs.use('./main', function(main) { main.init(); });
The use method can also load multiple modules at one time:
// 并发加载模块 a 和模块 b,并在都加载完成时,执行指定回调 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 required, the data-main attribute can be used to simplify it:
<script src="path/to/sea.js" data-main="./main"></script>
The above code is equivalent to:
<script src="path/to/sea.js"></script> <script> seajs.use('./main'); </script>
SeaJS also provides data-config to load Configuration file:
<script src="path/to/sea.js" data-config="path/to/config"></script>
data-config Equivalent:
seajs.config({ preload: ['path/to/config'] });
The path resolution rules are consistent with seajs.use.
What I use here is:
<script src="/js/lib/sea.js" data-config="/js/config.js"></script> <script> seajs.use('/js/main', function(main) { main.banner_focus('#focus'); });
Note: main is the module name. main.method is a function defined by the module, and parameters can be passed there.
Related recommendations:
Several commonly used functions and configurations in seajs
The above is the detailed content of How to use the use function in SeaJS. For more information, please follow other related articles on the PHP Chinese website!