包裹,如何修复“ regeneratorRuntime未定义”错误

我在一个项目中遇到了这个问题巴别塔我添加了一个async函数,但对于任何最新的JavaScript功能,问题都是相同的:

通天塔,由包裹,会生成一个polyfill,但是要避免发生此错误,您还需要加载regenerator-runtime运行。

一种解决方案:添加到主JavaScript文件的顶部:

import 'regenerator-runtime/runtime'

Parcel will include this package by default, increasing the size of 25KB.

The solution that is the most efficient in terms of codebase is adding the browserslist property to your package.json.

For example:

"browserslist": [
  "last 1 Chrome version"
]

For testing is good enough. To support multiple browsers:

"browserslist": [
  "last 3 and_chr versions",
  "last 3 chrome versions",
  "last 3 opera versions",
  "last 3 ios_saf versions",
  "last 3 safari versions"
]

or also:

"browserslist": [
  "since 2017-06"
]

You have to add a version that’s recent enough to support async/await, so Babel does not try to add a polyfill.

Check all the valid values here: https://github.com/browserslist/browserslist


More js tutorials: