angular – 异步函数仅在定位ECMAScript 2015或更高版本时可用

我希望能够使用async关键字,但我希望我的typescript编译成es5代码,以便我的代码可以在普通人浏览器中运行.当我尝试在visual studio代码中使用async时出现此错误.

Async functions are only available when targeting ECMAScript 2015 or
higher.

我认为针对es6意味着编译打字稿的输出javascript将是es6,而es6在大多数浏览器上都不起作用了吗?

这是我的tsconfig.json:

{
  "compilerOptions": {
    "target": "es5","module": "system","moduleResolution": "node","lib": ["es2015","dom"],"sourceMap": true,"emitDecoratorMetadata": true,"experimentalDecorators": true,"removeComments": false,"noImplicitAny": true,"outDir":"js/app/","typeRoots": [
      "./node_modules/@types"
    ]
  },"exclude": [
    "node_modules","app/test"
  ]
}

我想知道lib字段是否可以使它工作,因为它说“es2015”但它没有.

我也在想也许core-js shim可以使异步可用,但我只在我的index.html中包含了这样的垫片:

< script src =“node_modules / core-js / client / shim.min.js”>< / script>

所以我不认为它在编译时使异步可用.有没有更好的方法来导入core-js?

如何使异步可用,但也没有我的代码无法在大多数浏览器上运行?

大多数主流浏览器都支持ECMAScript6.

正如@loganfsmyth对此答案的评论所述,

Just keep in mind that “most major browsers” Could still only cover a
tiny percentage of your user base.

Firefox

Chrome

From Safari’s website,

ES6 The ECMAScript 2015 standard,also kNown as ES6,is completely
supported,bringing this major JavaScript evolution to Safari on macOS
and iOS.

你将不得不改变,

"target": "es5",

"target": "es6",

TypeScript 1.7 async/await support in ES6 targets (Node v4+) TypeScript Now supports asynchronous functions for engines that have native support for ES6 generators,e.g. Node v4 and above. Asynchronous functions are prefixed with the async keyword; await suspends the execution until an asynchronous function return promise is fulfilled and unwraps the value from the Promise returned.

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...