reactjs babel 7不允许在decorator和class之间使用export关键字,请使用`export @dec class`

nc1teljy  于 2023-04-05  发布在  React
关注(0)|答案(4)|浏览(134)

我使用react js和最新的babel7。当我使用mobx的装饰器时,我得到了错误
解析错误:不允许在装饰器和类之间使用export关键字。请改用export @dec class
我的代码

import React, { Component } from 'react';
import { observer,inject } from 'mobx-react'

import './style.scss'

@inject('routingStore', 'UserStore')
@observer
export default class Login extends Component {
    constructor(props) {
        super(props)
        this.state = {

        }
    }

    render() {

        return (
            <div>
                <h1>Login</h1>
            </div>
        )
    }
}

package.json

{
  "name": "admin-managment-barber",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@babel/core": "7.1.0",
    "@svgr/webpack": "2.4.1",
    "babel-core": "7.0.0-bridge.0",
    "babel-eslint": "9.0.0",
    "babel-jest": "23.6.0",
    "babel-loader": "8.0.4",
    "babel-plugin-named-asset-import": "^0.2.3",
    "babel-preset-react-app": "^6.1.0",
    "bfj": "6.1.1",
    "case-sensitive-paths-webpack-plugin": "2.1.2",
    "chalk": "2.4.1",
    "css-loader": "1.0.0",
    "dotenv": "6.0.0",
    "dotenv-expand": "4.2.0",
    "eslint": "5.6.0",
    "eslint-config-react-app": "^3.0.5",
    "eslint-loader": "2.1.1",
    "eslint-plugin-flowtype": "2.50.1",
    "eslint-plugin-import": "2.14.0",
    "eslint-plugin-jsx-a11y": "6.1.2",
    "eslint-plugin-react": "7.11.1",
    "file-loader": "2.0.0",
    "fork-ts-checker-webpack-plugin-alt": "0.4.14",
    "fs-extra": "7.0.0",
    "html-webpack-plugin": "4.0.0-alpha.2",
    "identity-obj-proxy": "3.0.0",
    "jest": "23.6.0",
    "jest-pnp-resolver": "1.0.1",
    "jest-resolve": "23.6.0",
    "mini-css-extract-plugin": "0.4.3",
    "mobx": "^5.6.0",
    "mobx-react": "^5.4.2",
    "mobx-react-router": "^4.0.5",
    "moment": "^2.22.2",
    "optimize-css-assets-webpack-plugin": "5.0.1",
    "pnp-webpack-plugin": "1.1.0",
    "postcss-flexbugs-fixes": "4.1.0",
    "postcss-loader": "3.0.0",
    "postcss-preset-env": "6.0.6",
    "postcss-safe-parser": "4.0.1",
    "react": "^16.6.3",
    "react-app-polyfill": "^0.1.3",
    "react-dev-utils": "^6.1.1",
    "react-dom": "^16.6.3",
    "react-router-dom": "^4.3.1",
    "resolve": "1.8.1",
    "sass-loader": "7.1.0",
    "style-loader": "0.23.0",
    "terser-webpack-plugin": "1.1.0",
    "url-loader": "1.1.1",
    "webpack": "4.19.1",
    "webpack-dev-server": "3.1.9",
    "webpack-manifest-plugin": "2.0.4",
    "workbox-webpack-plugin": "3.6.3"
  },
  "scripts": {
    "start": "node scripts/start.js",
    "build": "node scripts/build.js",
    "test": "node scripts/test.js"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,ts,tsx}",
      "!src/**/*.d.ts"
    ],
    "resolver": "jest-pnp-resolver",
    "setupFiles": [
      "react-app-polyfill/jsdom"
    ],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
      "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,ts,tsx}"
    ],
    "testEnvironment": "jsdom",
    "testURL": "http://localhost",
    "transform": {
      "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
      "^.+\\.module\\.(css|sass|scss)$"
    ],
    "moduleNameMapper": {
      "^react-native$": "react-native-web",
      "^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "web.ts",
      "ts",
      "web.tsx",
      "tsx",
      "json",
      "web.jsx",
      "jsx",
      "node"
    ]
  },
  "babel": {
    "plugins": [
      [
        "@babel/plugin-proposal-decorators",
        {
          "legacy": true
                }
      ],
      [
        "@babel/plugin-proposal-class-properties",
        {
          "loose": true
        }
      ]
    ],
    "presets": [
      "react-app"
    ]
  },
  "devDependencies": {
    "@babel/plugin-proposal-class-properties": "^7.1.0",
    "@babel/plugin-proposal-decorators": "^7.1.6",
    "@babel/plugin-transform-react-jsx-source": "^7.0.0"
  }
}

我想这可能是babel配置或什么的。我坚持这个问题了几个小时,我也试图降级babel到6.23.0,但没有成功。

kwvwclae

kwvwclae1#

您可以在.eslintrc文件中使用legacyDecorators选项,而不是在整个项目中更改import语句。

{    
  "parserOptions": {
     "ecmaFeatures": {
       "legacyDecorators": true
     }
   }
}
wgxvkvu9

wgxvkvu92#

有一个你可以做的工作,把你的类修改成下面的。

import React, { Component } from 'react';
import { observer,inject } from 'mobx-react'

import './style.scss'

@inject('routingStore', 'UserStore')
@observer
class Login extends Component {
  constructor(props) {
    super(props);
    this.state = {};
  }

  render() {
    return (
        <div>
            <h1>Login</h1>
        </div>
      );
    }
  }

export default Login;

从技术上讲,你只需要将导出移到类的底部。如果你有很多类,这个解决方案不是最好的。我还没有找到更好的方法。

pkmbmrz7

pkmbmrz73#

这对我很有效(使用create-react-app和customize-cra,并启用装饰器)

import React, { Component } from 'react';

export default @connect(
  store => ({
    //...
  }),
  dispatch => ({
    //...
  }),
)
class MyComponent extends Component {
  render() {
    return (
      <div>
        //...
      </div>
    );
  }
}
qyzbxkaa

qyzbxkaa4#

Typescript使用遗留装饰器。如这里所述,要正确地使typescript和babel使用装饰器处理相同的代码,您需要配置@babel/plugin-proposal-decorators也使用遗留装饰器,如下所示:

//babel.config.js
module.exports = {
  //...
  plugins: [['@babel/plugin-proposal-decorators',{legacy:true}]],
};

这与下面的tsconfig.json一起应该可以顺利工作

{
  "compilerOptions": {
    "experimentalDecorators": true,
   }
}

(其他答案提到了eslint,但人们可能会也可能不会使用它。)

相关问题