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

Several commonly used functions and configurations in seajs

小云云
小云云Original
2018-01-26 10:22:401342browse

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': '...'
 }
});

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

3. define

is used to define modules.


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

4. require

The interface used to obtain the specified module


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

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

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() {};
});

7. module.exports

is used to provide interfaces internally to the outside world


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

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!

Statement:
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