1、$ is not a function
原因:jquery默认遵循amd书写规范。
解决办法:需要修改jQuery文件,搜索到define.amd,并 修改成 (define.amd || define.cmd),让其也支持如果在seajs.config的alias中给jquery配置了别名叫'jq'或其他,则确保别名一定要叫'jquery'。
原因: seajs有可能不按seajs.use方法中配置的顺序来加载模块,seajs把bootstrap.js在jquery.js之前加载了。
解决办法:在入口模块内,使用require.async('bootstrap');(这里的'bootstrap'来自seajs.config里的alias配置)
3. // 判断是否有 Sea.js 等 CMD 模块加载器存在
if (typeof define === "function" && define.cmd) {
// 有
}
4. // 以下define等价于使用对象作为define函数参数的定义。 define( {foo: 'bar', doSomething: function(){} } );
define(function(require) {
// 通过 return 直接提供接口
return {
foo: 'bar',
doSomething: function() {}
};
});
5.特别注意:下面这种写法是错误的!
define(function(require, exports) {
// 错误用法!!!
exports = {
foo: 'bar',
doSomething: function() {}
};
});
正确的写法是用 return 或者给 module.exports 赋值:
define(function(require, exports, module) {
// 正确写法
module.exports = {
foo: 'bar',
doSomething: function() {}
};
});
6. 注意:对 module.exports 的赋值需要同步执行,不能放在回调函数里。下面这样是错误的:
define(function(require, exports, module) {
// 错误用法
setTimeout(function() {
module.exports = { a: "hello" };
}, 0);
});