【发布时间】:2020-01-16 19:30:46
【问题描述】:
当我在 IE 浏览器中运行我的应用程序时,我收到此错误:
我的应用在 Chrome 和 Firefox 中运行良好。
【问题讨论】:
-
IE 不支持 es6/es2015 语法(例如 lambda 函数箭头),因此您不能使用 es6 作为目标。在
tsconfig.json中将目标设置为 es5。 -
我正在使用目标 es2015
当我在 IE 浏览器中运行我的应用程序时,我收到此错误:
我的应用在 Chrome 和 Firefox 中运行良好。
【问题讨论】:
tsconfig.json 中将目标设置为 es5。
您必须启用 polyfill 才能使大部分功能在 IE 中运行。
【讨论】:
请参考我在this thread的回复:
默认情况下,在 Angular 版本 8 中,启用了 ng build 的差异加载。但是对于ng test 和ng serve,它只会生成一个无法在 IE11 中运行的 ES2015 构建。
您可以参考以下步骤配置 ES5 与 Angular 8 应用程序,这将使应用程序在 IE11 中运行。
Create a new tsconfig tsconfig-es5.app.json next to tsconfig.app.json with the below contents:
{
"extends": "./tsconfig.app.json",
"compilerOptions": {
"target": "es5"
}
}
In your angular.json add two new configuration section (es5 node) under the build and serve target to provide a new tsconfig.
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
...
},
"configurations": {
"production": {
...
},
"es5": {
"tsConfig": "./tsconfig-es5.app.json"
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
...
},
"configurations": {
"production": {
...
},
"es5": {
"browserTarget": "<your application name>:build:es5"
}
}
},
You can then run the serve with this configuration using the below command:
ng serve --configuration es5
另外,browserslist文件内容如下:
> 0.5%
last 2 versions
Firefox ESR
not dead
IE 9-11 # For IE 9-11 support, remove 'not'.
然后您可以使用以下命令使用此配置运行服务:
ng serve --configuration es5
【讨论】: