require.js - 詳解
阿新 • • 發佈:2018-09-08
插件 com 配置 return seconds bsp jquer http 就會
測試結構如下
index.html
1 <!DOCTYPE html>
2 <html lang="en">
3
4 <head>
5 <meta charset="UTF-8">
6 <meta name="viewport" content="width=device-width, initial-scale=1.0">
7 <meta http-equiv="X-UA-Compatible" content="ie=edge">
8 < title>Document</title>
9 <script data-main="js/app.js" src="https://cdn.bootcss.com/require.js/2.3.5/require.js"></script>
10 </head>
11
12 <body>
13
14 <!--
15 版本優化:默認有引入,例如用了jq,就會默認要求加入jquery.js,無需配置.
16 -->
17
18 <button id="require" >點擊require確定啊</button>
19
20 <input type="text" id="date3" data-options="{‘type‘:‘YYYY-MM-DD hh:mm‘,‘beginYear‘:2010,‘endYear‘:2088}" style="width:166px;height:19px;">
21
22
23 </body>
24
25 </html>
app.js
1 requirejs.config({
2 // 默認項目地址
3 baseUrl: ‘js/lib‘,
4
5 // 路徑(可本地可網絡)-去除後綴(默認.js),數組可填寫多個路徑,為了防止cdn突然失效
6 paths: {
7 ‘jquery‘: [‘jquery‘, ‘https://cdn.bootcss.com/jquery/3.3.1/jquery.min‘],
8 ‘jquery.date‘: [‘jquery.date‘],
9 ‘math‘: [‘../math‘]
10 },
11
12
13 // 依賴(jquery.date就依賴於jquery)
14 shim: {
15 ‘jquery.date‘: {
16 deps: [‘jquery‘],
17 exports: ‘jQuery.fn.date‘
18 }
19 },
20
21 // 控制插件依賴->jq($)
22 // map: {
23 // ‘*‘: { ‘jquery‘: ‘jquery.date‘ },
24 // ‘jquery.date‘: { ‘jquery‘: ‘jquery‘ }
25 // },
26
27
28 // 控制版本號
29 urlArgs: ‘ver=0.0.1‘,
30
31 // 請求等待時間
32 waitSeconds: 10
33 });
34
35
36
37 // app.js可以直接寫function和用.
38 function addCount(x, y) {
39 return x, y;
40 }
41
42
43 requirejs(["jquery", "jquery.date"],
44 function ($) {
45 // 邏輯代碼
46 $(‘#require‘).on(‘click‘, function () {
47 console.log(‘hehei‘);
48 });
49 // 初始化日期
50 $.date(‘#date3‘);
51 53
54 console.log($);
55 }
56 );
57
58
59
60 // 使用外部的js呢?(很抱歉,得另起一行了)
61 require([‘math‘], function (math) {
62 alert(math.add(1, 1));
63 });
資料參考於:https://blog.csdn.net/bluesky1215/article/details/71079667、http://www.requirejs.cn/
require.js - 詳解