1. 下载require.js
2. 引用
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Sample Project</title>
<!-- data-main attribute tells require.js to load scripts/main.js after
require.js loads.-->
<!--防止页面失去响应 defer async="true"-->
<script data-main="scripts/main" src="scripts/require.js" defer async="true"></script>
</head>
<body> </body>
</html>
入口文件main.js
// 使用require()加载所有需要运行的scripts
// 确保所有的scripts都是在这里加载的
require.config({
paths: {
'jquery': 'lib/jquery',
'underscore': 'lib/underscore',
'backbone': 'lib/backbone'
}
}); require(['test', 'jquery'], function (test, $) {
console.log(test.a(1, 1));
});
引入的模块文件:
define(function () {
var a = function (x, y) {
return x + y;
}; return {
a: a
}
});