AngularJs2與AMD加載器(dojo requirejs)集成


現在是西太平洋時間凌晨,這個問題我鼓搗了一天,都沒時間學英語了,英語太差,相信第二天我也看不懂了,直接看結果就行。
核心原理就是require在AngularJs2編譯過程中是關鍵字,而在瀏覽器里面運行時候AngularJs2不會去關心require方法。所以只要在原始的ts代碼中不出現require就沒問題。所以在AMD的加載器加載完畢之后,立即用一個全局變量保存,在AngularJs2的代碼中使用這個全局變量在做加載器。
Because require is a key word of Angular, RequireJs will conflict with Angularjs.
The reason angular use commonjs modular specification, it need to compile to js for run in browsers.
If you used require in code angularjs2 , the expression will be compiled to another style:

require(['dojo/_base/html'], (html) => {
      console.log(html);
    }); 

var html = __webpack_require__(['dojo/_base/html']);
console.log(html);

But if you the code in runing, the require will works fine.
image
This means Angularjs2 never replace the require when your app run in browsers.
So there have a perfect way to integrate requireJs within AngularJs2.

  <script>
    dojoConfig = {
      async: true,
      parseOnLoad: false
    }
  </script>
  <script src="//js.arcgis.com/3.17/"></script>
  <script type="text/javascript">
    dojoRequire = require;
  </script>

image


免責聲明!

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



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