[处理未定义或null?抛出SyntaxError:使用Webpack出现意外令牌

问题描述 投票:0回答:1

我已经使用?处理了未定义的内容?在我的代码的各个地方。早先使用script标签将代码直接添加到html中。在浏览器上,代码工作正常。现在正在使用webpack进行构建。

但是构建时出现以下错误:

Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: Unexpected token (57:29)

  55 |     computed: {       
  56 |         userRole(){
> 57 |             return this.user?.role || 'N/A';

在我的webpack配置中,模块是:

module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
        },
      },
    ],
  }

我也在使用es2015预设。知道如何解决这个问题...

提前谢谢

javascript webpack babel
1个回答
0
投票

我设法解决了可选的链接运算符问题。

我在package.json中具有更新依赖项,如下所示

"devDependencies": {
    "@babel/cli": "^7.8.4",
    "@babel/core": "^7.9.6",
    "@babel/plugin-proposal-optional-chaining": "^7.9.0",
    "@babel/preset-env": "^7.9.6",
    "babel-loader": "^8.1.0",
    "html-webpack-plugin": "^4.3.0",
    "webpack": "^4.43.0",
    "webpack-cli": "^3.3.11",
    "webpack-dev-server": "^3.10.3"
  }

&我的webpack配置为

module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ['@babel/preset-env'],
            plugins: ["@babel/plugin-proposal-optional-chaining"]
          }
        },
      },
    ],
  },

[我以前使用的是babel-clibabel-core等。更改为@babel/...以及将babel-loader更改为> 8后,东西也可以工作了。 Babel-loader在这里非常重要。

© www.soinside.com 2019 - 2024. All rights reserved.