可使用 config
方法来配置seajs。html
seajs.config({
alias: { //~~~相似requirejs的paths
'es5-safe': 'es5-safe/0.9.2/es5-safe',
'json': 'json/1.0.1/json',
'jquery': 'jquery/1.7.1/jquery'
},
preload: [
Function.prototype.bind ? '' : 'es5-safe',
this.JSON ? '' : 'json'
],
debug: true,
map: [
['http://example.com/js/app/', 'http://localhost/js/app/']
],
base: 'http://example.com/path/to/libs/', //~~~相似requirejs的baseUrl
charset: 'utf-8',
timeout: 20000
});
支持如下配置选项:jquery
当模块标识很长时,可使用 alias
配置来简化。~~~设置路径别名json
seajs.config({
alias: {
'app': 'http://path/to/app',
'jquery': 'jquery/1.7.1/jquery'
}
});
a.js:bootstrap
define(function(require, exports, module) {
var $ = require('jquery');
//=> http://path/to/libs/jquery/1.7.1/jquery.js
var biz = require('app/biz');
//=> http://path/to/app/biz.js
});
解析某个模块标识时,若是不想解析别名,能够在标识前面添加一个井号(#
):浏览器
define(function(require, exports, module) {
var $ = require('#jquery');
//=> http://path/to/libs/jquery.js
});
使用 preload
配置项,能够在普通模块加载前,提早加载并初始化好特定模块。app
// 在老浏览器中,提早加载好 ES5 和 json 模块:
seajs.config({
preload: [
Function.prototype.bind ? '' : 'es5-safe',
this.JSON ? '' : 'json'
]
});
preload 中的空字符串会被忽略掉。ide
值为 true
时,加载器会使用 console.log
输出全部警告和错误。 默认为 false
, 加载器只抛出异常。requirejs
另外,还能够将 debug
值设为 2
. 这种状况下, 每一个脚本请求都会加上惟一时间戳。这在测试期间颇有用,能够强制浏览器每次都请求最新版本。测试
该配置可将某个文件映射到另外一个。可用于在线调试,很是方便。更多信息,请参考 映射插件。ui
SeaJS 在解析顶级标识时,会相对 base
路径来解析。详情请参阅 顶级标识。
** 注意:请不要配置 base 路径,除非加载器没法自动获取。详情请参考 加载方式。
获取模块文件时,<script>
标签的 charset
属性。 默认是 utf-8
。
加载器等待脚本加载的最长时间。单位为毫秒,默认值是 20000(20秒)。
为了不冲突,或者须要定制全局命名空间以符合本身的口味时,可使用 noConflict
方法来实现。 ~~~相似jquery.noConflict
var myLoader = seajs.noConflict(); myLoader.use('./main'); /* main.js */ define(function(require, exports, module) { // snip... });
还能够经过给该方法传递 true
,来释放 define 方法。 不多会有这么作的必要, 请三思然后行。
var myLoader = seajs.noConflict(true); myLoader.use('./main'); /* main.js */ myLoader.define(function(require, exports, module) { // snip... });