在 jsx 上使用 eslint 进行缩进

IT技术 reactjs eslint eslintrc
2021-05-23 19:58:04

我正在尝试制作简单的组件。但是当我点击ctrl + s它时:

在此处输入图片说明

警告和错误是这样的:

[eslint] 预期的结束标记与开头的缩进相匹配。(react/jsx-closure-tag-location) [eslint] 预期缩进 4 个空格字符,但发现 2 个。 (react/jsx-indent)

我的.eslintrc

{
  "extends": "airbnb",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true,
      "experimentalObjectRestSpread": true
    }
  },
  "rules": {
    "max-len": ["warn", 120],
    "indent": ["warn", 2],
    "react/jsx-indent": ["warn", 4],
    "react/forbid-prop-types": 0,
    "semi": [2, "never"],
    "no-underscore-dangle": 0,
    "no-console": 0,
    "linebreak-style": 0,
    "comma-dangle": [2, {
      "arrays": "never",
      "objects": "always",
      "imports": "never",
      "exports": "never",
      "functions": "ignore"
    }]
  },
  "globals": {
    "localStorage": true
  },
  "env": {
    "browser": true,
    "node": true
  }
}

怎么了?

1个回答

在您的 gif 中,永远不会以相同的缩进关闭。那应该这样做:

const App = () => (
  <button>
    <span>asd</span>
  </button>
)

在你的 gif 你有

const App = () => (
   <button>
     <span>asd</span>
     </button>
)

或者

const App = () => (
     <button>
       <span>asd</span>
   </button>
)