Home > Web Front-end > JS Tutorial > How to use the use function in SeaJS

How to use the use function in SeaJS

小云云
Release: 2018-01-26 10:24:56
Original
2389 people have browsed it

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>
Copy after login
Copy after login

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();
});
Copy after login

The use method can also load multiple modules at one time:

// 并发加载模块 a 和模块 b,并在都加载完成时,执行指定回调
seajs.use(['./a', './b'], function(a, b) {
a.init();
b.init();
});
Copy after login

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>
Copy after login

The above code is equivalent to:

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

SeaJS also provides data-config to load Configuration file:

<script src="path/to/sea.js" data-config="path/to/config"></script>
Copy after login

data-config Equivalent:

seajs.config({
preload: ['path/to/config']
});
Copy after login

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');
});
Copy after login

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!

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