【问题标题】:How do I support Internet Explorer in an Angular 8 application?如何在 Angular 8 应用程序中支持 Internet Explorer?
【发布时间】:2019-05-30 13:13:32
【问题描述】:

当我使用 Angular CLI (8.0.0) 生成项目时,我运行 ng serve,在 Internet Explorer 中打开应用程序,然后出现一个空白屏幕。

我查看了polyfills.ts 文件并取消了以下行的注释:

    import 'classlist.js';
    import 'web-animations-js';

我还删除了所有 core.js 导入,因为 Angular 8 直接支持 core.js 3.0。

我也跑过npm i

package.json:

"dependencies": {
    "@angular/animations": "~8.0.0",
    "@angular/common": "~8.0.0",
    "@angular/compiler": "~8.0.0",
    "@angular/core": "~8.0.0",
    "@angular/forms": "~8.0.0",
    "@angular/platform-browser": "~8.0.0",
    "@angular/platform-browser-dynamic": "~8.0.0",
    "@angular/router": "~8.0.0",
    "classlist.js": "^1.1.20150312",
    "rxjs": "~6.4.0",
    "tslib": "^1.9.0",
    "web-animations-js": "^2.3.1",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.800.0",
    "@angular/cli": "~8.0.0",
    "@angular/compiler-cli": "~8.0.0",
    "@angular/language-service": "~8.0.0",
    "@types/node": "~8.9.4",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "^5.0.0",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.4.3"
  }

tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "esnext",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  }
}

编辑:

浏览器列表:

# This file is used by the build system to adjust CSS and JS output to support the specified browsers below.
# For additional information regarding the format and rule options, please see:
# https://github.com/browserslist/browserslist#queries

# You can see what browsers were selected by your queries by running:
#   npx browserslist

> 0.5%
last 2 versions
Firefox ESR
not dead
IE 9-11 # For IE 9-11 support, remove 'not'.

编辑 2:

Internet Explorer (11) 中的控制台显示以下错误:

polyfills.js: Syntax error (3168, 5) (第 3168 行开头)-> class Zone {

vendor.js: Syntax error (156, 1) (第 156 行开头)-> class PlatformLocation {

main.ts: Syntax error (95, 20) (指向 AppComponent)

我还需要做什么?

【问题讨论】:

  • 在浏览器列表中,将not IE 9-11替换为IE 9-11
  • 编辑:屏幕仍然保持空白,并在polyfills.jsvendor.jsmain.js@JBNizet 中产生 3 个语法错误
  • 我明白 - 我已经更新了这个问题。 @JBNizet

标签: angular angular-cli polyfills angular8


【解决方案1】:

根据this issue reply

您需要按照以下步骤操作

  1. 创建一个新的 tsconfig tsconfig.es5.json 在 tsconfig.json 旁边,内容如下
{
 "extends": "./tsconfig.json",
 "compilerOptions": {
     "target": "es5" 
  }
}
  1. angular.jsonprojects:yourAppName:architect:build:configurations 下,添加
"es5": {
      "tsConfig": "./tsconfig.es5.json"
    }

projects:yourAppName:architect:serve:configurations添加

    "es5": {
      "browserTarget": "yourAppName:build:es5"
    }

记得将 app:build:es5 中的 yourAppName 更改为 yourAppName

完整路径如下所示

"build": {
  "builder": "@angular-devkit/build-angular:browser",
  "options": {
      ...
  },
  "configurations": {
    "production": {
        ...
    },
    "es5": {
      "tsConfig": "./tsconfig.es5.json"
    }
  }
},
"serve": {
  "builder": "@angular-devkit/build-angular:dev-server",
  "options": {
      ...
  },
  "configurations": {
    "production": {
     ...
    },
    "es5": {
      "browserTarget": "yourAppName:build:es5"
    }
  }
},
  1. 使用以下命令使用此配置运行服务。
ng serve --configuration es5

【讨论】:

  • 如果ng serve --configuration es5 不起作用,请尝试ng s -c=es5
  • 我必须像这样提供 tsconfig 路径:"tsConfig": "src/tsconfig-es5.app.json"
  • 如果我只使用 ng build 需要这个吗?
  • 这在我使用 ng build 时不起作用。如何在 ie11 上构建我的应用程序?
  • 如果我在angular.json中有projects > projectName > architect > build > options > styles,是否需要将其复制到projects > projectName > architect > build > configurations > es5?问题是我的应用程序已加载,但缺少样式。
【解决方案2】:

转到 "tsconfig.json" 并使用 target: "es5" 而不是 "target": "es2015",

compileOnSave\compilerOptions 中的目标

【讨论】:

  • 这会禁用 Angular 的“差异加载”功能。所以最后的包大小(浏览器下载)“总是(独立于浏览器)”比需要的大。
  • 这个解决方案对我不起作用。从 IE11 浏览器控制台中的 vendor.js 获取“无效字符”。已尝试此页面中的所有解决方案。从3天开始挣扎。任何帮助将非常感激。有关问题描述和项目详细信息的更多信息 stackoverflow.com/questions/64386606/
【解决方案3】:

------简单易行的方法

您需要更改 2 个文件,分别为 polyfills.tstsconfig.json

只需在 polyfills.ts 中添加 Browser Polyfills

/***************************************************************************************************
* BROWSER POLYFILLS
*/
/** IE9, IE10 and IE11 requires all of the following polyfills. **/
import 'core-js/es6/symbol';
import 'core-js/es6/promise';
import 'core-js/es6/object';
import 'core-js/es6/function';
import 'core-js/es6/parse-int';
import 'core-js/es6/parse-float';
import 'core-js/es6/number';
import 'core-js/es6/math';
import 'core-js/es6/string';
import 'core-js/es6/date';
import 'core-js/es6/array';
import 'core-js/es6/regexp';
import 'core-js/es6/map';
import 'core-js/es6/weak-map';
import 'core-js/es6/set';

并在tsconfig.json 将“目标”更改为 es5 像这样

 "target": "es5", 

而不是

"target": "es2015",

【讨论】:

  • 请注意:core-js/es6/* 不起作用,而是使用core-js/es/*。只需将es6 更改为es
  • 这个解决方案对我不起作用。从 IE11 浏览器控制台中的 vendor.js 获取“无效字符”。已尝试此页面中的所有解决方案。从3天开始挣扎。任何帮助将非常感激。有关问题描述和项目详细信息的更多信息 stackoverflow.com/questions/64386606/
  • @Raju,请先在 incongito 模式下检查,然后你可以删除你的 node_module 和 npm cache clean -f 然后 npm install。希望对你有帮助
【解决方案4】:

注意:我最初的回复来自 Reddit (https://www.reddit.com/r/Angular2/comments/buup6a/)

检查你的 tsconfig.json 在升级到 Angular 8 时,我的目标更改为 es2015,所以使用 ng serve 我遇到了很多问题。编译时,dist 文件夹有 es5 和 es2015 版本。

基本上,为生产编译会为新旧浏览器(如 IE11)创建两个版本

为了在开发环境中测试 IE11,我在 angular.json 中创建了一个开发环境,在其中我指定了 tsconfig 的副本,我将其命名为 tsconfig.dev.json,其中目标设置为 es5。运行 ng s -c=dev 和瞧!

【讨论】:

  • 太棒了,谢谢。我在 angular-cli GitHub 存储库中发现了一个相关问题,其中提到了 Jie Lin 所说的:github.com/angular/angular-cli/issues/14455 很烦人,这在任何地方都没有记录
  • 这个解决方案对我不起作用。从 IE11 浏览器控制台中的 vendor.js 获取“无效字符”。已尝试此页面中的所有解决方案。从3天开始挣扎。任何帮助将非常感激。有关问题描述和项目详细信息的更多信息 stackoverflow.com/questions/64386606/
【解决方案5】:

为了完全支持 IE,我们必须从 mdn-polyfills 库中引入一组特殊的 polyfill。

要安装它们,请使用

npm i -s mdn-polyfills

接下来像这样将它们添加到 polyfills.ts 文件中

import 'mdn-polyfills/Object.assign';
import 'mdn-polyfills/Object.create';
import 'mdn-polyfills/Object.entries';
import 'mdn-polyfills/Array.from';
import 'mdn-polyfills/Array.of';
import 'mdn-polyfills/Array.prototype.find';
import 'mdn-polyfills/Array.prototype.forEach';
import 'mdn-polyfills/Array.prototype.filter';
import 'mdn-polyfills/Array.prototype.findIndex';
import 'mdn-polyfills/Array.prototype.includes';
import 'mdn-polyfills/String.prototype.includes';
import 'mdn-polyfills/String.prototype.repeat';
import 'mdn-polyfills/String.prototype.startsWith';
import 'mdn-polyfills/String.prototype.endsWith';
import 'mdn-polyfills/String.prototype.padStart';
import 'mdn-polyfills/String.prototype.padEnd';
import 'mdn-polyfills/Function.prototype.bind';
import 'mdn-polyfills/NodeList.prototype.forEach';
import 'mdn-polyfills/Element.prototype.closest';
import 'mdn-polyfills/Element.prototype.matches';
import 'mdn-polyfills/MouseEvent';
import 'mdn-polyfills/CustomEvent';

在此之后,您应该停止在 IE 中遇到许多问题。

【讨论】:

  • 您能解释一下您必须安装 mdn-polyfills 的原因吗?
  • 对于 Angular 8,导入 MDN polyfill 并不能解决问题。
  • 在更新到 Angular 8 后遇到 IE 11 无法加载我的应用程序的问题,我注意到控制台中出现 Object.entries 错误。按照这里的说明,它解决了我的问题。
  • @doubleya Angular 8 中的差异加载应该支持开箱即用的 ES 2015 功能。您只需为 ES2017 功能(如 Object.values 和 Object.entries)添加 polyfill。除了 mdn-polyfills,您还可以使用 import 'core-js/es/object/values'import 'core-js/es/object/entries'
【解决方案6】:

我只想添加一些对我有用的东西:

  1. 从官方 Angular 文档中阅读这篇文章:https://angular.io/guide/browser-support
  2. 另一篇文章在你的过程中帮助你:https://dev.to/paulinhapenedo/angular-8-and-ie-11-1ed2
  3. 对我来说是帮助我理解一切的最好的文章,甚至是前两篇:https://medium.com/angular-in-depth/angular-and-internet-explorer-a6e4b5a5dae1

简而言之,如果您不想再阅读,我已采取以下步骤:

  • 取消注释 polyfill.ts 文件中的一些导入。

    导入'classlist.js';

    导入'web-animations-js';

  • 安装几个 npm 包。

npm install --save classlist.js npm install --save web-animations-js

  • 修改 browserslist 文件。在文件末尾查找这一行:

    not IE 9-11 # 对于 IE 9-11 支持,删除 'not'。

请去掉'not'字。

另外只需在 polyfills.ts 中添加 Browser Polyfills

/***************************************************************************************************
* BROWSER POLYFILLS
*/
/** IE9, IE10 and IE11 requires all of the following polyfills. **/
import 'core-js/es/symbol';
// import 'core-js/es/promise';
import 'core-js/es/object';
import 'core-js/es/function';
import 'core-js/es/parse-int';
import 'core-js/es/parse-float';
import 'core-js/es/number';
import 'core-js/es/math';
import 'core-js/es/string';
import 'core-js/es/date';
import 'core-js/es/array';
import 'core-js/es/regexp';
import 'core-js/es/map';
import 'core-js/es/weak-map';
import 'core-js/es/set';

并在tsconfig.json 中将“目标”更改为 es5 像这样

“目标”:“es5”,

而不是

“目标”:“es2015”,

运行:

ng 服务 --open

希望对你有帮助:)

【讨论】:

    【解决方案7】:

    target设置为“es5”,设置你的browserslist,就够了。

    在此处阅读有关该主题的更多信息:https://blog.ninja-squad.com/2019/05/29/angular-cli-8.0/

    【讨论】:

    • 这个解决方案对我不起作用。从 IE11 浏览器控制台中的 vendor.js 获取“无效字符”。已尝试此页面中的所有解决方案。从3天开始挣扎。任何帮助将非常感激。有关问题描述和项目详细信息的更多信息 stackoverflow.com/questions/64386606/
    【解决方案8】:

    您只需要对 Angular 8 进行三处更改。 更改 polyfills.ts 和 tsconfig.json 。 添加一个名为 tsconfig-es5.json 的新文件,其内容如下..

    {
     "extends": "./tsconfig.json",
     "compilerOptions": {
     "target": "es5" 
     }
     }
    

    将 tsconfig.json 中的目标更改为 “目标”:“es5”,

    执行以下命令“ng serve”

    【讨论】:

    • 这个解决方案对我不起作用。从 IE11 浏览器控制台中的 vendor.js 获取“无效字符”。已尝试此页面中的所有解决方案。从3天开始挣扎。任何帮助将非常感激。有关问题描述和项目详细信息的更多信息 stackoverflow.com/questions/64386606/
    【解决方案9】:

    我遇到了类似的问题,并通过以下两个修复程序解决了。您可能会看到很多关于 polyfill 的帖子,但这并不是 IE 无法按预期工作的唯一原因。

    解决方案

    在 angular.json 文件中添加属性 "es5BrowserSupport": true。路径如图所示。

    现在,在 tsconfig.json 文件中将 target 属性更改为“es5

    完整的解释可以找到here

    通过应用这两个修复程序,IE 将开始在本地(调试)和生产环境中工作。

    【讨论】:

    • 这适用于我使用 Visual Studio .NET Core 3 模板 - 只需将目标更改为“es5”即可 - 尽管我将它用于功能相当低的 POC
    • 这个解决方案对我不起作用。从 IE11 浏览器控制台中的 vendor.js 获取“无效字符”。已尝试此页面中的所有解决方案。从3天开始挣扎。任何帮助将非常感激。有关问题描述和项目详细信息的更多信息 stackoverflow.com/questions/64386606/
    【解决方案10】:

    这真的让我很困扰,所以我写了一个nodeJs script,它将启用此处定义的“解决方法”:ng github

    作为一个开发出大量 Angular 应用程序的企业开发人员,仅针对 chrome 和 firefox 进行本地开发是不行的。任何做过网络开发超过一分钟的人都知道,仅仅因为它在 chrome 中看起来很酷并不意味着 IE 会高兴。好的,只需安装脚本并不时在 IE 中提供它,然后在推送到 dev 之前在 IE 中本地检查您的应用程序。

    【讨论】:

    • 这太棒了!不适用于 NX 工作区,因为带有 browserslisttsconfig.* 的应用程序位于 apps/app-name 中,而 angular.json 位于工作区根目录中。在应用程序的文件夹中运行它,仍然节省了一些时间,只需要手动编辑 angular.json。那谢谢啦! :)
    【解决方案11】:

    这是一个非常酷的 Angular 新功能,称为 差异加载

    <script src=“runtime-es2015.858f8dd898b75fe86926.js” type=“module”></script>
    <script src=“polyfills-es2015.e954256595c973372414.js” type=“module”></script>
    <script src=“runtime-es5.741402d1d47331ce975c.js” nomodule></script>
    <script src=“polyfills-es5.405730e5ac8f727bd7d7.js” nomodule></script>
    <script src=“main-es2015.63808232910db02f6170.js” type=“module”></script>
    <script src=“main-es5.2cc74ace5dd8b3ac2622.js” nomodule></script>
    

    如果你从build文件夹看到上面的index.html,每个js都有两个版本:

    • 一个是 es2015,带有 type="module"attribute 用于现代浏览器
    • 另一个是带有 nomodule 属性的 es5 版本,用于延迟浏览器。

    因此,Angular cli 会在 prod 上优雅地生成它。但是如果你想从本地运行ng serve,那么你必须手动确保ng serve是从一个具有target: es5的tsconfig文件运行的

    【讨论】:

      【解决方案12】:

      第 1 步: 在 polyfills.ts 中取消注释 plyfills。同时运行 polyfills.ts 文件中提到的所有 npm install 命令来安装这些包

      第 2 步: 在 browserslist 文件中删除没有提到 IE 9-11 支持的地方

      第 3 步: 在 tsconfig.json 文件中将 "target": "es2015" 更改为 "target": "es5"

      这些步骤解决了我的问题

      【讨论】:

        【解决方案13】:

        更新包含的browserslist 并删除`IE 9-11 ...之前的not 怎么样?像这样?

        此文件被构建系统用来调整 CSS 和 JS 输出以支持以下指定的浏览器。 有关格式和规则选项的更多信息,请参阅: https://github.com/browserslist/browserslist#queries

        您可以通过运行查看查询选择了哪些浏览器:

        npx browserslist
        

        0.5% 最后 2 个版本 火狐 ESR 没死 IE 9-11 # 对于 IE 9-11 支持,删除 'not'。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-04-15
          • 1970-01-01
          • 1970-01-01
          • 2012-05-21
          • 2016-07-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-18
          相关资源
          最近更新 更多