1.基础结构
2.在index.html文件中引入
<script data-main="scripts/main" src="lib/require.js"></script>
3.入口文件main.js
require.config({
baseUrl: '',
paths: {
jquery: ['https://cdn.bootcss.com/jquery/3.3.1/jquery','lib/jquery'],
hello: 'scripts/hello'
}
});
require(['jquery', 'hello'],function ($, hello) {
$("#btn").click(function(){
hello.showMessage("hangge.com");
});
});
4.hello.js
define(['jquery'],function($){
//变量定义区
var moduleName = "hello module";
var moduleVersion = "1.0";
//函数定义区
var showMessage = function(name){
if(undefined === name){
return;
}else{
$('#messageBox').html('欢迎访问 ' + name);
}
};
//暴露(返回)本模块API
return {
"moduleName":moduleName,
"version": moduleVersion,
"showMessage": showMessage
}
});