简体   繁体   English

如何让 @babel/plugin-proposal-private-methods 在 vue.js webpack 模板中工作?

[英]How to make @babel/plugin-proposal-private-methods work in the vue.js webpack template?

I have an app based on a vue.js webpack template.我有一个基于 vue.js webpack 模板的应用程序。 I needed to use the syntax of private fields and private methods of classes.我需要使用私有字段的语法和类的私有方法。 For this, I installed @babel/plugin-proposal-private-methods ^7.4.4 and @babel/core^7.0.0.为此,我安装了@babel/plugin-proposal-private-methods ^7.4.4 和@babel/core^7.0.0。 After installing the packages, I tried to build a development version, but I got the following errors.安装软件包后,我尝试构建开发版本,但出现以下错误。

Webpack version is ^3.6.0, vue ^2.6.10, babel-core ^6.22.1, babel-preset-env ^1.3.2. Webpack 版本为 ^3.6.0,vue ^2.6.10,babel-core ^6.22.1,babel-preset-env ^1.3.2。


    ERROR in ./assets/main.js
    Module build failed: Error: Requires Babel "^7.0.0-0", but was loaded with "6.26.3". If you are sure you have a compatible version of @babel/core, it is likely that something in your build process is loading the wrong version. Inspect the stack trace of this error to look for the first entry that doesn't mention "@babel/core" or "babel-core" to see what is calling Babel.

It looks like you are loading @babel/core@7.XX with babel 6.看起来您正在使用 babel 6 加载 @babel/core@7.XX。

In other words you are calling the core compiler of babel seven with the api of babel 6.换句话说,您正在使用 babel 6 的 api 调用 babel 7 的核心编译器。

Releasing babel 7, the babel team went on a major revamp and it's great but not retro compatible.发布 babel 7,babel 团队进行了重大改造,它很棒但不兼容复古。

Your template was probably working with babel 6 and you need some babel 7 plugin.你的模板可能与 babel 6 一起工作,你需要一些 babel 7 插件。 So to sum ip up you'll have to:所以总结一下 ip 你必须:

  • upgrade all your babel dependencies (cli, core, plugins, presets ...)升级所有 babel 依赖项(cli、core、插件、预设...)
  • update your babel config更新你的 babel 配置
  • replace the deprecated替换已弃用的
  • probably upgrade your webpack babel loader or at least fix the webpack conf可能升级你的 webpack babel loader 或者至少修复 webpack conf

I highly encourage you to read the official upgrading documentation: https://babeljs.io/docs/en/v7-migration You also might want to upgrade webpack.我强烈建议您阅读官方升级文档: https ://babeljs.io/docs/en/v7-migration 您可能还想升级 webpack。

cheers干杯

Add the following to your nuxt.config.js file under the build section.将以下内容添加到 build 部分下的 nuxt.config.js 文件中。

build: {
  babel:{
    plugins: [
      ['@babel/plugin-proposal-private-methods', { loose: true }]
    ]
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM