大家好,我是H5女王,最近因为vscode默认格式化代码被小小的折腾了一下。

那现在教大家一个让vscode可以根据eslint的配置格式化代码的方法。

首先,在vscode下载eslint扩展和prettier扩展

其次,在vscode的user settings 中进行如下配置:

```

{

"liveServer.settings.donotShowInfoMsg": true,

"explorer.confirmDelete": false,

"vetur.validation.template": false,

"vetur.format.defaultFormatter": {

"html": "prettier",

"css": "prettier",

"postcss": "prettier",

"scss": "prettier",

"less": "prettier",

"js": "prettier",

"ts": "prettier",

"stylus": "stylus-supremacy"

},

"javascript": "javascriptreact",

"vue-html": "html",

"plaintext": "jade",

"editor.insertSpaces": true,

"git.ignoreMissingGitWarning": true,

"editor.formatOnPaste": true,

"editor.formatOnSave": true,

"files.associations": {

"*.wxml": "html"

},

"files.autoSave": "onFocusChange",

"workbench.startupEditor": "newUntitledFile",

"eslint.enable": true,

"eslint.autoFixOnSave": true,

"eslint.validate": [

"javascript",

"javascriptreact",

{

"language": "html",

"autoFix": true

},

{

"language": "vue",

"autoFix": true

}

],

"git.confirmSync": false,

"git.decorations.enabled": false,

"git.detectSubmodules": false,

"git.enabled": false,

"editor.fontSize": 18,

"editor.formatOnType": false,

"editor.tabSize": 4,

"prettier.eslintIntegration": true

}

```

这样就可以了,希望大家不再为vscode格式化代码而烦恼哦~