Angular 9应用程序无法在IE11上运行。

5
我们团队创建了一个 Angular 9 的新项目,在 Google Chrome 和 Firefox 上运行良好,但在 IE11 上无法显示任何内容。 我已经尝试了互联网上的所有方法,遵循了与此相关的每个博客,但我无法实现目标。
以下是我的 tsconfig.json。
{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
      "@app/*": ["src/app/*"],
      "@modules/*": ["src/app/modules/*"],
      "@services/*": ["src/app/services/*"],
      "@shared/*": ["src/app/shared/*"],
      "@env/*": ["src/environments/*"],
      "@mocks/*": ["mocks/*"],
      "@models/*": ["src/app/models/*"]
    }
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

tsconfig-es5.app.json

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

package.json

{
  "name": "connect-ui",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve --port 6574",
    "start:dev": "concurrently \"npm run mocks\" \"npm start\"",
    "mocks": "json-server --watch db.json --routes db.routes.json --port 6575",
    "build": "ng build --base-href /ng/",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "storybook": "start-storybook -p 6576",
    "build-storybook": "build-storybook"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^9.1.9",
    "@angular/common": "~9.1.0",
    "@angular/compiler": "~9.1.0",
    "@angular/core": "~9.1.0",
    "@angular/forms": "~9.1.0",
    "@angular/localize": "~9.1.0",
    "@angular/platform-browser": "~9.1.0",
    "@angular/platform-browser-dynamic": "~9.1.0",
    "@angular/router": "~9.1.0",
    "@fortawesome/angular-fontawesome": "^0.6.1",
    "@fortawesome/fontawesome-svg-core": "^1.2.28",
    "@fortawesome/free-brands-svg-icons": "^5.13.0",
    "@fortawesome/free-regular-svg-icons": "^5.13.0",
    "@ng-bootstrap/ng-bootstrap": "^6.1.0",
    "@ngx-gallery/core": "^5.0.0-beta.0",
    "@ngx-translate/core": "^12.1.2",
    "@ngx-translate/http-loader": "^4.0.0",
    "@types/zingchart": "^2.8.0",
    "angular-gridster2": "^9.1.0",
    "angular2-uuid": "^1.1.1",
    "bootstrap": "^4.4.0",
    "classlist.js": "^1.1.20150312",
    "concurrently": "^5.2.0",
    "core-js": "^2.5.5",
    "flatpickr": "^4.6.3",
    "json-server": "^0.16.1",
    "moment": "^2.26.0",
    "ng2-flatpickr": "^9.0.0",
    "normalize.css": "^8.0.1",
    "resize-observer-polyfill": "^1.5.1",
    "rxjs": "~6.5.4",
    "shortcut-buttons-flatpickr": "^0.3.0",
    "tslib": "^1.10.0",
    "web-animations-js": "^2.3.2",
    "zingchart": "^2.9.0",
    "zingchart-angular": "0.0.6",
    "zinggrid": "^1.2.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.901.7",
    "@angular/cli": "~9.1.0",
    "@angular/compiler-cli": "~9.1.0",
    "@angular/language-service": "~9.1.0",
    "@babel/core": "^7.10.3",
    "@storybook/addon-actions": "^5.3.19",
    "@storybook/addon-docs": "^5.3.19",
    "@storybook/addon-knobs": "^5.3.19",
    "@storybook/addon-links": "^5.3.19",
    "@storybook/addon-notes": "^5.3.19",
    "@storybook/addons": "^5.3.19",
    "@storybook/angular": "^5.3.19",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "babel-loader": "^8.1.0",
    "codelyzer": "^5.1.2",
    "ember-cli-cors": "0.0.2",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.4.1",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.0",
    "karma-jasmine": "~3.0.1",
    "karma-jasmine-html-reporter": "^1.4.2",
    "protractor": "~5.4.3",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~3.8.3"
  }
}

我已经查看了这个链接:https://medium.com/better-programming/how-to-fix-your-angular-app-when-its-not-working-in-ie11-eb24cb6d9920。我在其他帖子中看到一些用户在IE11的控制台中出现了错误,但是我的控制台窗口并没有出现任何错误。请查看IE11的这张屏幕截图:enter image description hereenter image description here。pollyfills.ts。
/***************************************************************************************************
 * Load `$localize` onto the global scope - used if i18n tags appear in Angular templates.
 */
import '@angular/localize/init';
/**
 * This file includes polyfills needed by Angular and is loaded before the app.
 * You can add your own extra polyfills to this file.
 *
 * This file is divided into 2 sections:
 *   1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers.
 *   2. Application imports. Files imported after ZoneJS that should be loaded before your main
 *      file.
 *
 * The current setup is for so-called "evergreen" browsers; the last versions of browsers that
 * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera),
 * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile.
 *
 * Learn more in https://angular.io/guide/browser-support
 */

/***************************************************************************************************
 * BROWSER POLYFILLS
 */






/** IE10 and IE11 requires the following for NgClass support on SVG elements */
 import 'classlist.js';  //Run `npm install --save classlist.js`.


 import 'core-js/es6/reflect';



/**
 * Web Animations `@angular/platform-browser/animations`
 * Only required if AnimationBuilder is used within the application and using IE/Edge or Safari.
 * Standard animation support in Angular DOES NOT require any polyfills (as of Angular 6.0).
 */
import 'web-animations-js';  // Run `npm install --save web-animations-js`.

/**
 * By default, zone.js will patch all possible macroTask and DomEvents
 * user can disable parts of macroTask/DomEvents patch by setting following flags
 * because those flags need to be set before `zone.js` being loaded, and webpack
 * will put import in the top of bundle, so user need to create a separate file
 * in this directory (for example: zone-flags.ts), and put the following flags
 * into that file, and then add the following code before importing zone.js.
 * import './zone-flags';
 *
 * The flags allowed in zone-flags.ts are listed here.
 *
 * The following flags will work for all browsers.
 *
 * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame
 * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick
 * (window as any).__zone_symbol__UNPATCHED_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
 *
 *  (window as any).__Zone_enable_cross_context_check = true;
 *
 */

/***************************************************************************************************
 * Zone JS is required by default for Angular itself.
 */
import 'zone.js/dist/zone';  // Included with Angular CLI.


/***************************************************************************************************
 * APPLICATION IMPORTS
 */

---------------EDIT--------------------

enter image description here

enter image description here

enter image description here

enter image description here


你看过 pollyfills.js 吗?里面有一些针对IE的东西,你可能需要取消注释。 - mwilson
@mwilson,我已经取消了注释。 - Karthik Saxena
网络标签页里有什么奇怪的东西吗? - MikeOne
我已经编辑了我的问题,并添加了网络选项卡和pollyfils.ts。 - Karthik Saxena
1
根据您的polyfills.js文件,我发现您已经添加了@angular/localize/init的导入,但是该函数使用'let'和'const'来定义函数,因此不支持IE11浏览器。请从您的polyfills.js文件中删除localize的导入,这样您就可以在IE11浏览器中运行您的项目了。 - Jaypal Sodha
7个回答

8

你是否编辑过 browserslist 文件? 你应该在 IE 9-11 前面删除 not。browserslist 如下所示:

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

我还制作了一个新的 Angular 9 应用程序,并且它在 IE 11 中可以很好地工作。我添加了一个新的 tsconfig-es5.app.json 文件,更新了 angular.json 配置,编辑了 browserslist 然后运行 ng serve --configuration es5。结果是像这样的。此外,您不需要在 polyfills.ts 中取消注释。
我的 tsconfig.json:
{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

我的package.json文件:
{
  "name": "angular9",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~9.1.0",
    "@angular/common": "~9.1.0",
    "@angular/compiler": "~9.1.0",
    "@angular/core": "~9.1.0",
    "@angular/forms": "~9.1.0",
    "@angular/platform-browser": "~9.1.0",
    "@angular/platform-browser-dynamic": "~9.1.0",
    "@angular/router": "~9.1.0",
    "rxjs": "~6.5.4",
    "tslib": "^1.10.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.901.0",
    "@angular/cli": "~9.1.0",
    "@angular/compiler-cli": "~9.1.0",
    "@angular/language-service": "~9.1.0",
    "@types/node": "^12.11.1",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "^5.1.2",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.4.1",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.0",
    "karma-jasmine": "~3.0.1",
    "karma-jasmine-html-reporter": "^1.4.2",
    "protractor": "~5.4.3",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~3.8.3"
  }
}

1
是的,我已经编辑了browserlist,并且我也制作了tsconfig.app.json,在其中设置了es5配置以支持IE上的js,还添加了Meta标记,但仍然没有成功。 - Karthik Saxena
请不要取消注释 polyfills.ts 中的 polyfills。我尝试过,如果我取消注释并安装包,它将无法在 IE 中加载。然后您可以清除 IE 缓存并重试。 - Yu Zhou
1
是的,您可以尝试将 polyfills.ts 恢复到其原始状态。在我的 polyfills.ts 文件中,只有这一行 import 'zone.js/dist/zone'; 是取消注释的。据我所知,Angular 8+ 不需要取消注释 polyfills.ts 就能使其与 IE 兼容。 - Yu Zhou
1
似乎您的Angular应用程序并不是一个新的空白应用程序。问题似乎与配置无关,而是某些语法与IE不兼容。我建议您通过类似于[StackBlitz](https://stackblitz.com/)的在线代码编辑器提供可重现的示例,以便我们可以进行测试并查看问题所在。 - Yu Zhou
感谢 @YuZhou 的帮助,在使用 es5 tsconfig 文件时,我还需要安装 core-js 以便在 IE11 上运行。输入命令 npm i -S core-js 安装后,我在 polyfills.js 中添加了这一行 import 'core-js'; ,然后就可以正常运行了。 - Rupesh Kumar Tiwari
显示剩余2条评论

7
修改以下两个内容即可使其正常工作(已在Angular 8和9版本上测试)。
- 在browserslist文件中,将not IE 9-11改为IE 9-11。 - 在tsconfig.json文件中,将"target": "es2015"改为"target": "es5"
大多数情况下,以上更改就足够了。如果仍然出现一些错误(可能是由于更复杂的UI/UX),可以尝试修改polyfills.ts文件,
- 取消注释// import 'classlist.js';// import 'web-animations-js'; - 必须npm install --save classlist.js web-animations-js

2
请从您的polyfills.js文件中删除以下行。
**/***************************************************************************************************
 * Load `$localize` onto the global scope - used if i18n tags appear in Angular templates.
 */
import '@angular/localize/init';**

它在IE11浏览器中也能正常工作。根据我查阅的一些文档,它说localize与es5不兼容。


0
请在项目的angular.json文件中的projects > architect > build下添加以下属性:
"es5BrowserSupport": true

2. 在tsconfig.json文件中将目标从 "target": "es2015" 改为 "target": "es5"


0

前往你的 polyfills.ts 文件,在 /** IE9, IE10 and IE11 requires all of the following polyfills. */ 部分下找到被注释掉的导入语句:

// import 'classlist.js';  // Run `npm install --save classlist.js`.

取消注释上面的导入,使您的应用程序与那些IE浏览器一起工作,但不要忘记运行以下命令。

npm install --save classlist.js

你可能还需要取消注释以下导入:

// import 'web-animations-js';  // Run `npm install --save web-animations-js`.

不要忘记运行以下命令:
npm install --save web-animations-js

如果上述过程不起作用,则可以尝试以下步骤并检查是否有效:)
在文件中,您可以将not IE 9-11替换为IE 9-11
此外,在tsconfig.json文件中,您可以将"target": "es2015"(或其他)替换为"target": "es5"

0
如果问题仍未解决,以下是步骤:
  1. browserlists.ts(取消注释IE11)
  2. tsconfig.ts(在目标中设置为“es5”,并添加lib:“es5” "target": "es5", "lib": [ "es2018", "es5", "dom" ]
完成后,在ng serve之前再次进行ng build。

-1
将以下代码放入 head 标签内的 index.html 文件中(projectName/src/index.html),然后重新运行您的应用程序(ng serve)。
<meta http-equiv="X-UA-Compatible" content="IE=edge" />

<meta http-equiv="X-UA-Compatible" content="IE=edge" /> - Namrata Das

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接