刚从9.0更新到Angular 10
现在,在我的代码中每次使用可选链接(https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining)都会导致以下错误的实例
ERROR in ./src/app/app.component.ts 39:18
Module parse failed: Unexpected token (39:18)
File was processed with these loaders:
* ./node_modules/@ngtools/webpack/src/index.js
You may need an additional loader to handle the result of these loaders.
| this.title = 'my-app';
| const x = this.GetObject();
> let y = x?.myvar;
| }我确认此错误仅在按照我的tsconfig.base.json文件中的如下所述以es2020为目标时发生,但在以es2019为目标时也没有问题
"target": "es2020", //If set to es2019 then all OK
"module": "es2020",
"lib": [
"es2018", //Error also occurs if this set to es2020
"dom"
],我确认这个错误发生在新生成的Angular 10应用程序中,如下所示,然后将目标更改为es2020 (所以这不是我的代码!)Typescript版本为3.9.7
npm install -g @angular/cli
ng new my-app我还注意到,在es2020的发行说明中,“可选链接”现在是一个新功能。这让我怀疑“可选链接”的Typescript编译不知何故没有与新的es2020功能结合起来?
如何在es2020中使用Optional-Chaining?
发布于 2020-07-27 12:20:22
在定位es2020时在Angular 10中使用可选链接将导致错误‘模块解析失败:意外令牌’
当前的解决方法是以es2019为目标
https://stackoverflow.com/questions/63065175
复制相似问题