【问题标题】:Angular 9 Application Not running on IE11Angular 9 应用程序未在 IE11 上运行
【发布时间】:2020-11-05 03:53:46
【问题描述】:

我们的团队创建了一个 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 控制台窗口中没有任何错误。 请查看 IE11 的截图。

polyfills.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
 */

---------------编辑--------

【问题讨论】:

  • 你看过pollyfills.js吗?那里有一些特定于 IE 的内容,您可能需要取消注释。
  • @mwilson 我已经取消注释了..
  • 网络标签有什么奇怪的吗?
  • 我已经编辑了我的问题并添加了网络选项卡和 pollyfils.ts
  • 正如我在您的 polyfills.js 文件中看到的,您添加了对 @angular/localize/init 的导入,它不支持 IE11 浏览器,因为此函数使用 'let' 和 'const ' 来定义函数。从你的 polyfills.js 文件中删除 localize 的导入,你就可以在 IE11 浏览器中运行你的项目了。

标签: angular typescript internet-explorer-11 browser-support


【解决方案1】:

将以下代码放入头标签 (projectName/src/index.html) 内的 index.html 文件中并重新运行您的应用程序 (ng serve)

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

【讨论】:

【解决方案2】:

您是否编辑过 browserslist 文件?您应该在 IE 9-11 之前删除 not。浏览器列表如下:

> 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。结果是like this。此外,您不需要在 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"
  }
}

【讨论】:

  • 是的,我也编辑了浏览器列表,我也制作了 tsconfig.app.json,在其中我将 es5 作为配置,以便在 IE 上支持 js 也添加了 Meta 标签,但仍然没有运气。
  • 请不要取消注释 polyfills.ts 中的 polyfill。我试过如果我取消注释并安装包,它不会在 IE 中加载。然后你可以清空IE缓存再试一次。
  • 你的意思是说我应该再次注释掉classlist.js和web-animations-js?
  • 是的,您可以尝试将polyfills.ts 恢复到其原始状态。在我的polyfills.ts 中,只有这一行import 'zone.js/dist/zone'; 未注释。据我所知,Angular 8+ 不需要取消注释 polyfills.ts 即可使其与 IE 兼容。
  • 您的 Angular 应用似乎不是一个新的空白应用。该问题似乎与配置无关,但某些语法与 IE 不兼容。我建议您通过在线代码编辑器(如StackBlitz 或其他方式)提供可重现的示例,以便我们进行测试并查看问题所在。
【解决方案3】:

请从您的 polyfills.js 文件中删除以下行。

**/***************************************************************************************************
 * Load `$localize` onto the global scope - used if i18n tags appear in Angular templates.
 */
import '@angular/localize/init';**

它在 IE11 浏览器中也可以正常工作。正如我浏览过的一些文档所说,localize 与 es5 不兼容。

【讨论】:

    【解决方案4】:

    修改以下两件事将使其工作(在 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

    【讨论】:

      【解决方案5】:

      如果问题仍然没有解决,这里是步骤

      1. browserlists.ts(取消注释 IE11)
      2. tsconfig.ts(在目标中为“es5”并在库中添加:“es5”) “目标”:“es5”, “库”:[ "es2018", "es5", “dom” ]

      完成后,在 ng serve 之前再次 ng build

      【讨论】:

        【解决方案6】:

        1.在项目>架构师>构建中的angular.json文件中添加如下属性:

        "es5BrowserSupport": true
        

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

        【讨论】:

          【解决方案7】:

          转到您的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
          

          如果上述过程不起作用,那么您可以执行以下操作并检查它是否有效:)

          browserslist 文件中,您可以将not IE 9-11 替换为IE 9-11

          另外,在tsconfig.json 文件中,您可以将"target": "es2015"(或其他)替换为"target": "es5"

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-10-27
            • 2018-02-27
            • 2018-11-26
            • 1970-01-01
            • 2019-03-08
            • 1970-01-01
            相关资源
            最近更新 更多