定义公共模块类app,实现方法的复用
html:
1 <!DOCTYPE html>
2 <html lang="en">
3
4 <head>
5 <meta charset="UTF-8">
6 <title>demo</title>
7 </head>
8
9 <body>
10 <script data-main="./main" src="./require.min.js" defer async="true"></script>
11 </body>
12
13 </html>
main.js:
1 //异步加载app,demo模块
2 require.config({
3 baseUrl: './',
4 paths: {
5 'app': 'app',
6 'demo': 'demo'
7 },
8 shim: {
9 'demo': ['app']
10 }
11 })
12 require(['demo'], function(app) {
13
14 })
app.js:
1 //定义方法
2 define([], function() {
3
4 var service = {};
5
6 service.add = function(msg, callback) {
7 console.log(msg);
8 callback();
9 }
10
11 service.del = function() {
12 console.log('del')
13 }
14
15 return service;
16 });
demo.js:
1 //引入app模块,调用add方法
2 define(['app'], function(app) {
3
4 app.add("Hello World", function() {
5 console.log("成功");
6 });
7
8 })