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

posted @   木的树  阅读(2216)  评论(2编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示