eslint如何设置HTML和js缩进为2
时间: 2023-11-22 12:52:38 浏览: 44
要设置 eslint 的缩进为 2,需要在你的项目根目录下创建一个 `.eslintrc` 文件,如果已经存在可以直接在文件中添加以下配置:
```
{
"rules": {
"indent": ["error", 2],
"vue/html-indent": ["error", 2, {
"attribute": 1,
"baseIndent": 1,
"closeBracket": 0,
"alignAttributesVertically": true,
"ignores": []
}]
}
}
```
这里的 `indent` 规则是针对 js 文件的,`vue/html-indent` 规则是针对 vue 文件中的 HTML 代码的,具体的配置可以根据自己的需求进行调整。
需要注意的是,一些编辑器如 VSCode、WebStorm 等,也可以直接在编辑器中进行 eslint 配置。
相关问题
eslint如何设置HTML和js缩进为2 用tab缩进
要将 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。