Home > Web Front-end > JS Tutorial > Several commonly used functions and configurations in seajs

Several commonly used functions and configurations in seajs

小云云
Release: 2018-01-26 10:22:40
Original
1374 people have browsed it

This article mainly introduces the 7 most commonly used functions and configurations in seajs, and briefly analyzes the commonly used project configuration, module loading, definition, acquisition and other operating techniques in seajs in the form of examples. Friends in need can refer to the following. I hope Can help everyone.

1. seajs.config


seajs.config({
 // 设置路径,方便跨项目调用
 paths: {
  'path1': '....',
  'path2': '....'
 },
 // 设置别名,方便调用
 alias: {
  'class1': '...',
  'class2': '...'
 }
});
Copy after login

2. seajs.use

Used to load one or more modules in the page


// 加载一个模块
seajs.use('./a');
// 加载一个模块,在加载完成时,执行回调
seajs.use('./a', function(a) {
 a.doSomething();
});
// 加载多个模块,在加载完成时,执行回调
seajs.use(['./a', './b'], function(a, b) {
 a.doSomething();
 b.doSomething();
});
Copy after login

3. define

is used to define modules.


define(function(require, exports, module) {
 // 模块代码
});
Copy after login

4. require

The interface used to obtain the specified module


define(function(require) {
 // 获取模块a的接口
 var a = require('./a');
 // 调用模块a的方法
 a.doSomething();
});
Copy after login

5. require.async

is used to load one or more modules asynchronously inside the module


define(function(require) {
 // 异步加载一个模块,在加载完成时执行回调
 require.async('./b', function(b) {
  b.doSomething();
 });
 // 异步加载多个模块,在加载完成时执行回调
 require.async(['./c', './d'], function(c, d) {
  c.doSomething();
  d.doSomething();
 });
});
Copy after login

6. exports

is used to load one or more modules asynchronously inside the module Providing interfaces internally to the outside world


define(function(require, exports) {
 // 对外提供foo属性
 exports.foo = 'bar';
 // 对外提供doSomething方法
 exports.doSomething = function() {};
});
Copy after login

7. module.exports

is used to provide interfaces internally to the outside world


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

Related recommendations:

JavaScript modular development library SeaJS

The above is the detailed content of Several commonly used functions and configurations 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