浏览器支持

Browser support

Angular 支持大多数常用浏览器,包括下列版本:

Angular supports most recent browsers. This includes the following specific versions:

浏览器

Browser

支持的版本

Supported versions

Chrome

最新版

latest

Firefox

最新版

latest

Edge

最近的两个主版本

2 most recent major versions

IE

11
10
9

IE Mobile

11

Safari

最近的两个主版本

2 most recent major versions

iOS

最近的两个主版本

2 most recent major versions

Android

Nougat (7.0)
Marshmallow (6.0)
Lollipop (5.0, 5.1)
KitKat (4.4)

Angular 在持续集成过程中,对每一个提交都会使用 SauceLabsBrowserstack 在上述所有浏览器上执行单元测试。

Angular's continuous integration process runs unit tests of the framework on all of these browsers for every pull request, using SauceLabs and Browserstack.

腻子脚本 (polyfill)

Polyfills

Angular 构建于 Web 平台的最新标准之上。 要支持这么多浏览器是一个不小的挑战,因为它们不支持现代浏览器的所有特性。

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.

你可以通过加载腻子脚本("polyfills")来为想要支持的浏览器弥补这些特性。 下表 列出了可能用到的大多数腻子脚本。

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.

这些建议的腻子脚本是运行完整 Angular 应用所需的。 你可能还会需要另一些的腻子脚本来支持没有出现在此列表中的哪些特性。 注意,这些腻子脚本并没有神奇的魔力来把老旧、慢速的浏览器变成现代、快速的浏览器,它只是填充了 API。

The suggested polyfills are the ones that run full Angular applications. You may need additional polyfills to support features not covered by this list. Note that polyfills cannot magically transform an old, slow browser into a modern, fast one.

启用腻子脚本

Enabling polyfills

Angular CLI 的用户可以通过自动创建的 src/polyfills.ts 文件来启用这些腻子脚本。

Angular CLI users enable polyfills through the src/polyfills.ts file that the CLI created with your project.

这个文件把强制的和很多可选的腻子脚本组织成 JavaScript 的 import 语句。

This file incorporates the mandatory and many of the optional polyfills as JavaScript import statements.

强制性 腻子脚本(如 zone.js)的 npm 包在创建项目时就已经自动安装了,相应的 import 语句也都加好了。你一般不用动它们。

The npm packages for the mandatory polyfills (such as zone.js) were installed automatically for you when you created your project and their corresponding import statements are ready to go. You probably won't touch these.

但是如果要用一个可选的腻子脚本,就要通过 npmyarn 来安装它们的 npm 包了。 比如,如果你需要 web 动画的腻子脚本,就要通过下列命令之一来安装它:

But if you need an optional polyfill, you'll have to install its npm package. For example, if you need the web animations polyfill, you could install it with npm, using the following command (or the yarn equivalent):

# note that the web-animations-js polyfill is only here as an example # it isn't a strict requirement of Angular anymore (more below) npm install --save web-animations-js
      
      # note that the web-animations-js polyfill is only here as an example

# it isn't a strict requirement of Angular anymore (more below)

npm install --save web-animations-js
    

然后打开 polyfills.ts 文件,并反注释对应的 import 语句,就像这样:

Then open the polyfills.ts file and un-comment the corresponding import statement as in the following example:

/** * Required to support Web Animations `@angular/platform-browser/animations`. * Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation **/ import 'web-animations-js'; // Run `npm install --save web-animations-js`.
src/polyfills.ts
      
      /**

* Required to support Web Animations `@angular/platform-browser/animations`.

* Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation
**/
import 'web-animations-js';  // Run `npm install --save web-animations-js`.
    

如果在 polyfills.ts 中找不到要使用的腻子脚本,就可以仿照下列模式自行添加它:

If you can't find the polyfill you want in polyfills.ts, add it yourself, following the same pattern:

  1. 安装 npm 包

    install the npm package

  2. polyfills.tsimport 这个文件

    import the file in polyfills.ts

不使用 CLI 的用户可以遵循下列步骤自行操作。

Non-CLI users should follow the instructions below.

强制性腻子脚本

Mandatory polyfills

下表中的腻子脚本是每个浏览器中运行 Angular 应用时要用到哪些:

These are the polyfills required to run an Angular application on each supported browser:

浏览器(桌面和移动)

Browsers (Desktop & Mobile)

需要的腻子脚本

Polyfills Required

Chrome, Firefox, Edge, Safari 9+

ES7/reflect (仅 JIT)

ES7/reflect (JIT only)

Safari 7 & 8, IE10 & 11, Android 4.1+

ES6

IE9

ES6
classList

可选浏览器特性的腻子脚本

Optional browser features to polyfill

有些 Angular 特性可能需要额外的腻子脚本。

Some features of Angular may require additional polyfills.

例如,动画库依赖于标准的 web 动画 API,目前它只能用在 Chrome 和 Firefox 上。 (注意,在 Angular 中,只有当用到了 AnimationBuilder 时,对 web-animations-js 的依赖才是必要的。)

For example, the animations library relies on the standard web animation API, which is only available in Chrome and Firefox today. (note that the dependency of web-animations-js in Angular is only necessary if AnimationBuilder is used.)

下列特性可能需要更多腻子脚本:

Here are the features which may require additional polyfills:

特性

Feature

腻子脚本

Polyfill

浏览器(桌面和移动)

Browsers (Desktop & Mobile)

JIT 编译

JIT compilation.

需要 reflect 来提供元数据。

Required to reflect for metadata.

ES7/reflect

默认对目前的所有浏览器都启用了。如果总是使用 AOT 模式,并且只使用 Angular 自带的装饰器,那么可以移除它。

All current browsers. Enabled by default. Can remove if you always use AOT and only use Angular decorators.

动画
只有在应用中用到了 Animation Builder 时才需要;Angular 标准的动画支持是不需要任何腻子脚本的(截至 NG6)。

Animations
Only if Animation Builder is used within the application--standard animation support in Angular doesn't require any polyfills (as of NG6).

Web 动画

Web Animations

如果使用了 AnimationBuilder,那么腻子脚本将为 IE/Edge 和 Safari 启用擦除(scrubbing)支持(Chrome 和 Firefox 原生支持此特性)

If AnimationBuilder is used then the polyfill will enable scrubbing support for IE/Edge and Safari (Chrome and Firefox support this natively).

如果你使用下列已废弃的 i18n 管道:

If you use the following deprecated i18n pipes:

date,

currency,

decimal,

percent

Intl API

除了 Chrome、Firefox、Edge、IE11 和 Safari 10 外的所有浏览器

All but Chrome, Firefox, Edge, IE11 and Safari 10

NgClass

在 SVG 元素上应用时

on SVG elements

classList

IE10, IE11

Http

Http 发送和接收二进制数据时

when sending and receiving binary data

Typed Array

Blob

FormData

IE 9

建议的腻子脚本

Suggested polyfills

下表中是用来测试框架本身的腻子脚本,它们是应用程序的优质起点。

Below are the polyfills which are used to test the framework itself. They are a good starting point for an application.

腻子脚本

Polyfill

授权方式

License

大小*

Size*

ES7/reflect

MIT

0.5KB

ES6

MIT

27.4KB

classList

公共域

Public domain

1KB

Intl

MIT / Unicode license

13.5KB

Web Animations

Apache

14.8KB

Typed Array

MIT

4KB

Blob

MIT

1.3KB

FormData

MIT

0.4KB

* 这里的数据都按最小化并且 gzip 压缩后的版本算,是由closure compiler计算出的。

* Figures are for minified and gzipped code, computed with the closure compiler.

非 CLI 的用户的腻子脚本

Polyfills for non-CLI users

如果你不使用 CLI,就要直接把腻子脚本添加到宿主页(index.html)中,就像这样:

If you are not using the CLI, you should add your polyfill scripts directly to the host web page (index.html), perhaps like this.

<!-- pre-zone polyfills --> <script src="node_modules/core-js/client/shim.min.js"></script> <script src="node_modules/web-animations-js/web-animations.min.js"></script> <script> /** * you can configure some zone flags which can disable zone interception for some * asynchronous activities to improve startup performance - use these options only * if you know what you are doing as it could result in hard to trace down bugs.. */ // __Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame // __Zone_disable_on_property = true; // disable patch onProperty such as onclick // __zone_symbol__BLACK_LISTED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames /* * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js * with the following flag, it will bypass `zone.js` patch for IE/Edge */ // __Zone_enable_cross_context_check = true; </script> <!-- zone.js required by Angular --> <script src="node_modules/zone.js/dist/zone.js"></script> <!-- application polyfills -->
src/index.html
      
      <!-- pre-zone polyfills -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/web-animations-js/web-animations.min.js"></script>
<script>
  /**

   * you can configure some zone flags which can disable zone interception for some

   * asynchronous activities to improve startup performance - use these options only

   * if you know what you are doing as it could result in hard to trace down bugs..
   */
  // __Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame
  // __Zone_disable_on_property = true; // disable patch onProperty such as onclick
  // __zone_symbol__BLACK_LISTED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames

  /*

   * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js

   * with the following flag, it will bypass `zone.js` patch for IE/Edge
   */
  // __Zone_enable_cross_context_check = true;
</script>
<!-- zone.js required by Angular -->
<script src="node_modules/zone.js/dist/zone.js"></script>

<!-- application polyfills -->