要在微信小程序中只使用 ES6 语法,需要在项目的根目录下创建一个 '.babelrc' 文件,然后添加以下内容:

{
  'presets': [
    ['env', {
      'modules': false,
      'targets': {
        'browsers': ['> 1%', 'last 2 versions', 'not ie <= 8']
      }
    }]
  ],
  'plugins': []
}

然后在项目中安装 babel-loader 和 babel-preset-env:

npm install --save-dev babel-loader babel-preset-env

接下来,在项目的 webpack.config.js 文件中,添加以下配置:

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      }
    ]
  }
  // ...
}

这样,就可以在微信小程序中使用 ES6 语法了。

微信小程序 ES6 语法配置指南

原文地址: https://www.cveoy.top/t/topic/oxlT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录