Support for the experimental syntax 'classProperties' isn't currently enabled

项目中使用高级语法报错,

报错信息

SyntaxError: E:\workdata\webpackVue\src\index.js: Support for the experimental syntax 'classProperties' isn't currently enabled (22:8):

20 |
21 | class Dog {
> 22 | name = 'bigWhite'
| ^
23 | static color = 'yellow'
24 | }
25 | const d = new Dog()

Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' section of your Babel config to enable transformation.

这是因为在js中写了高级语法,浏览器不支持,需要安装babel插件,并配置,这里报错信息会提示我们安装插件

在项目中安装 npm i @babel/plugin-proposal-class-properties -D

在webpac.config.js文件中配置

module: {
    rules: [{
      test: /\.css$/,
      use: ['style-loader', 'css-loader']
    }, {
      test: /\.js$/,
      use: {
        loader: 'babel-loader',
        options: {
          presets: ['@babel/env'],
          plugins: ['@babel/plugin-proposal-class-properties']
        }
      }
    }]
  },

配置后就能正常运行,并且也会转化为普通语法

猜你喜欢

转载自www.cnblogs.com/steamed-twisted-roll/p/10963997.html