RequireJS 加載 js 執行順序


初次接觸RequireJS 對文檔理解不很透徹,自己通過測試測到的執行順序:

文檔結構:

|-amaze

  | -js

    | -amazeui.js

    | -jquery.min.js

    | -main.js

    | -test.js

  | index.html

 

index.html

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8" />
 5         <title>商城首頁</title>
 6         <script src="//cdn.bootcss.com/require.js/2.1.22/require.js" data-main="js/main"></script>
 7     </head>
 8     <script>
 9         alert("index-test前");
10         require(['test']);
11         alert("index-test后");
12     </script>
13     <body>
14         <div id="css"></div>
15     </body>
16     <script>
17         alert("body后");
18     </script>
19 </html>

 

main.js

 1 require.config({
 2     paths:{
 3         jquery:"jquery.min"
 4     },
 5     shim:{
 6         amazeui:{
 7             deps:['jquery']
 8         }
 9     }
10 })
11 
12 require(['jquery','amazeui'],function(){
13     alert("main")
14     var a = '測試';
15 });


test.js

1 define(['jquery.min','amazeui','require'],function(){
2     alert("test");
3     console.log($("#css").css({'width':'200px','height':'200px','background':'red'}));
4 });

 

執行index.html頁面,測試得出彈出順序為:

  head  =>  body后   =>  test.js  => main.js

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM