eslint抱怨getInitialProps

reactjs React.js

小宇宙

2020-03-24

这是我的.eslintrc

{
  "plugins": ["react"],
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "env": {
    "es6": true,
    "browser": true,
    "node": true,
    "mocha": true
  },
  "extends": ["eslint:recommended", "plugin:react/recommended", "standard"],
  "rules": {}
}

这是我的组件:

class Index extends React.Component {
  static async getInitialProps({ req }) {
    ....
  }
}

Eslint抱怨getInitialProps:

Parsing error: Unexpected token getInitialProps

除了添加抑制注释之外,还有什么方法可以使eslint接受getInitialProps声明?

第3469篇《eslint抱怨getInitialProps》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

1个回答
小宇宙LEY 2020.03.24

正如@Scott在评论中指出的那样,解决方案是添加babel-eslint,我的最终.eslintrc是以下内容:

{
  "plugins": ["react"],
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "env": {
    "es6": true,
    "browser": true,
    "node": true,
    "mocha": true
  },
  "extends": ["eslint:recommended", "plugin:react/recommended", "standard"],
  "rules": {}
}

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android