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

前端之家收集整理的这篇文章主要介绍了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.

猜你在找的Angularjs相关文章