首页 > 其他 > 详细

vue 项目中使用 async/await 报错:regeneratorRuntime is not defined

时间:2021-04-07 14:48:24      阅读:38      评论:0      收藏:0      [点我收藏+]

背景

基于 webpack 搭建的 vue 项目中,某个文件使用 async/await 报错,例如:

<script>
  export default {
    name: "test",
    methods: {
      async queryUsers() {
        // 配置请求参数
        const options = { url: this.$constant.BACK_API.QUERY_USRS }
        const users = await this.$request(options)
        console.log(users)
      }
    },
    mounted() {
      this.queryUsers()
    }
  }
</script>

原因

在 vue 项目中使用async/await处理并行多个异步,因为项目中没有使用 transform-runtimees6+ 转换成 es5

解决方案

安装 @babel/plugin-transform-runtime

npm i @babel/plugin-transform-runtime -D

配置 .babelrc 文件:

{
  "plugins": [
    [
      "@babel/plugin-transform-runtime"
    ]
  ]
}

若项目中不存在 .babelrc 文件,直接在项目根目录下添加即可。

vue 项目中使用 async/await 报错:regeneratorRuntime is not defined

原文:https://www.cnblogs.com/haveadate/p/14627111.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!