eslint如何设置HTML和js缩进为2 用tab缩进
时间: 2023-07-15 21:13:05 浏览: 52
要将 HTML 和 JavaScript 文件的缩进设置为 2,使用 tab 缩进,可以使用以下 eslint 配置:
```
{
"rules": {
"indent": ["error", 2, { "SwitchCase": 1 }],
"no-tabs": 0
},
"overrides": [
{
"files": ["*.html"],
"rules": {
"indent": ["error", 2]
}
}
]
}
```
这个配置中,我们设置了 `indent` 规则来指定缩进为 2,`SwitchCase` 表示在 switch 语句中 case 语句的缩进级别为 1。`no-tabs` 规则被禁用以允许使用 tab 缩进。
同时,我们使用了 `overrides` 来指定 .html 文件的缩进级别为 2,因为 HTML 文件的缩进通常与 JavaScript 文件不同。
相关问题
eslint如何设置vue 中的HTML 缩进为tab 2个tab缩进
可以在`.eslintrc.js`文件中的`rules`对象中添加以下规则:
```js
rules: {
// ...
'vue/html-indent': ['error', 'tab', {
'indentSwitchCase': true,
'ignores': []
}]
}
```
其中,`'vue/html-indent'`表示要设置的规则为 Vue 文件中的 HTML 缩进规则,`'error'`表示如果不符合规则就报错,`'tab'`表示使用 Tab 缩进,`2`表示缩进两个 Tab,`'indentSwitchCase': true`表示在 switch 语句中缩进 case 语句,`'ignores': []`表示不忽略任何节点。
如果你使用的是 VS Code,还需要在 VS Code 的设置中添加以下配置,才能生效:
```json
"editor.insertSpaces": false,
"editor.tabSize": 2
```
其中,`"editor.insertSpaces": false`表示使用 Tab 缩进,`"editor.tabSize": 2`表示缩进两个 Tab。
eslint报错"message": "Expected indentation of 0 spaces but found 4.",
这个错误提示是因为你的代码中有缩进错误。你需要在你的代码编辑器中设置正确的缩进格式。一般来说,JavaScript 中的缩进一般为 2 或 4 个空格。你可以在编辑器的设置中进行更改。如果你使用的是 VS Code,可以在设置中搜索 "tab size" 和 "indentation" 来进行设置。另外,你也可以在代码中使用 eslint 插件来自动修复缩进错误。