用于 es6 的 Angular Polyfill

标签 angular typescript ecmascript-6

我正在浏览下面列出的 Angular 文档以及网络上有关 Angular 的其他一些链接。如果所有代码都是用 Typescript 编写的,还需要 es6 polyfills 吗?另外我的理解是,最终 webpack 将代码转译为 ES5,那么 polyfills 在哪里使用呢?

https://angular.io/guide/browser-support

https://github.com/xiaomoinfo/Angular2/blob/master/how-angular-works/inventory-app/src/polyfills.ts

Ionic 3 - Polyfill for ES6 in IE11 Not Working

最佳答案

Angular 是使用现代浏览器中的功能构建的。当在现代浏览器中运行时,它使用该浏览器的内置功能。<​​/p>

为了在没有这些内置功能的旧版浏览器中运行,您可以添加“polyfills”,将新功能添加到旧版浏览器中。

一些与旧版浏览器的兼容性问题可以通过转译来解决,例如将 ES6 语言语法转译回可在 ES5 中使用的语法。但是,其他功能(例如 ES6 中的 Map 对象)需要进行多填充,以便可以在旧版浏览器中使用。

来自 Angular 上的链接:

Angular is built on the latest standards of the web platform. Targeting such a wide range of browsers is challenging because they do not support all features of modern browsers.

You compensate by loading polyfill scripts ("polyfills") for the browsers that you must support. The table below identifies most of the polyfills you might need.


这种设计方法为您提供了针对最新浏览器进行优化的代码设计,但可以通过一些额外的工作(包括一些填充)来与旧版浏览器一起使用。

诸如 jQuery 早期版本之类的库选择了不同的方法。他们没有使用 polyfill,而是创建了自己所需功能的实现(例如延迟),并在所有代码中使用它们,无论浏览器如何。虽然这对于开发人员来说很容易部署,但随着时间的推移,它会遇到问题。 jQuery 代码变得臃肿,因为它添加了现在内置于现代浏览器的代码,并且某些 jQuery 实现与现代标准(例如 Promises)不兼容。


因此,Angular 的某些功能需要在 ES6 中实现的库。转译并不会消除对这些库的需要。他们仍然需要存在。这就是 Polyfill 的用途。使用 Angular,您可以决定要支持哪个级别的旧版浏览器,然后添加所需的 Polyfill。 Angular 不会自动为您执行此操作。

关于用于 es6 的 Angular Polyfill,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49392722/

相关文章:

node.js - ng 构建不带 Node 模块下载

angular6 特性模块延迟加载抛出错误TypeError : undefined is not a function

javascript - typescript 有没有办法推断数组中不再可能有空值?

javascript - 单击另一个组件后无法解决组件中的参数错误

javascript - ES6 捆绑 CSS 的方式?

javascript - Object.assign 没有按预期工作

Angular 2拦截路由事件然后路由

include - 如何正确地将 mdi 包含到 Angular2 webpack 中

javascript - 如何在 Angular 6 中处理多个 http 请求

javascript - jQuery 代码需要转换为 vanilla 以便我可以在 Angular 中使用