VSCode配置格式化工具(Prettier/Vetur/ESLint)和jsconfig.json

本文详细介绍如何在VSCode中配置格式化工具,包括Prettier、Vetur和ESLint,涵盖插件安装、配置文件设置、代码片段创建及常用命令脚本配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

VSCode配置格式化工具(Prettier/Vetur/ESLint)

网上很多配置,有的过时了, 有的很杂, 自己看了下文档,简单配置了以下, 顺便记录下来。
准备
vue-cli建好项目之后,在VSCode安装插件
ESLint document,Prettier - Code formatter document, Vetur document
cli install
Launch VS Code Quick Open (Ctrl+P)

ext install esbenp.prettier-vscode

配置
.eslintrc.js使用vue-cli默认生产的就行

新建一个.prettierrc.js

需要什么配置, 自己对文档改

module.exports = {
  trailingComma: 'es5',
  printWidth: 100,
  tabWidth: 2,
  useTabs: false,
  semi: false,
  singleQuote: true,
  jsxSingleQuote: true,
  bracketSpacing: true,
  jsxBracketSameLine: true,
  arrowParens: "avoid"
}

.eslintrc.js
使用默认的
.eslintignore
可以配置ignore文件

/public/
/dist/
/node_modules/
/src/icons/svg/
/mock/

VSCode的配置settings.json

注意: The old eslint.autoFixOnSave setting is now deprecated and can safely be removed.

{
    "workbench.iconTheme": "vscode-icons",
    "files.autoSave": "afterDelay",
    "editor.fontSize": 16,
    "editor.mouseWheelZoom": true,
    "editor.suggest.snippetsPreventQuickSuggestions": false,
    "liveServer.settings.donotShowInfoMsg": true,
    "workbench.colorTheme": "Material",
    "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
    "editor.tabSize": 2,
    "editor.tabCompletion": "on",
    // 下面这几个, 怎么配看文档
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "eslint.enable": true,
    "editor.codeActionsOnSave": {
        "source.fixAll": false,
        "source.fixAll.eslint": true //保存自动修复, eslint.autoFixOnSave已废弃
    }
    
}

如果要配置js-beautify之类的,自己看下文档, 我就不配那么多东西了, 合适够用就好了。

配置script命令

  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint", //自带的修复
    "lint-check": "eslint --ext .js,.vue src", // 检查
    "lint-fix": "eslint --fix --ext .js,.vue src" // 修复
  },

运行yarn lint-check/fix可以检查/修复,yarn lint修复会有修复文件的提示

配置jsconfig.json

参考vertur
路径映射:如用@引入模块会有路径提示

import module from '@/components/module'

jsconfig.json

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "baseUrl": "./",
    "paths": {
      "components/*": ["src/components/*"],
      "@/*": ["src/*"]
    }
  },
  "exclude": ["node_modules"],
  "include": ["src/*"]
}

或者使用tsconfig.json

配置常用代码片段

在VSCode’文件’->‘首选项’->‘用户代码片段’,新建代码片段
defaultSinppets.code-snippets.json

{
	// Place your 全局 snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and 
	// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope 
	// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is 
	// used to trigger the snippet and the body will be expanded and inserted. Possible variables are: 
	// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. 
	// Placeholders with the same ids are connected.
	// Example:
	// "Print to console": {
	// 	"scope": "javascript,typescript",
	// 	"prefix": "log",
	// 	"body": [
	// 		"console.log('$1');",
	// 		"$2"
	// 	],
	// 	"description": "Log output to console"
	// }
		"Print to console": {
		"scope": "javascript,typescript",
		"prefix": "clg",
		"body": [
			"console.log('$1')",
			"$2"
		],
		"description": "Log output to console"
	},
	"Print to dir": {
		"scope": "javascript,typescript",
		"prefix": "cd",
		"body": [
			"console.dir('$1')",
			"$2"
		],
		"description": "Log output to dir"
	}
}

希望对你有帮助, 有问题请留言

<think>嗯,用户想配置VSCode,让它在自动保存时用Prettier格式化代码。我之前看过几个相关的引用,现在得仔细看看这些引用里的信息,然后整理出步骤。首先,引用[3]提到要设置Prettier为默认格式化工具,然后在设置里开启保存时自动格式化。引用[4]也说到了editor.formatOnSave这个选项需要勾选。还有引用[2]提到配置.prettierrc.cjs文件后要重启VSCode,否则可能会有缓存问题。 那步骤应该是先安装Prettier插件,然后在VSCode的设置里找到默认格式化程序,选择Prettier。接着开启保存时格式化的选项。可能还需要配置.prettierrc文件来自定义规则,比如引号或分号。用户可能还会遇到问题,比如其他插件冲突或者需要重启的情况。需要提醒用户检查是否有冲突的扩展,或者是否需要重启VSCode配置生效。另外,如果用户的项目里有.editorconfig文件,也可能影响格式化的结果,需要确认Prettier是否优先使用自己的配置。总之,步骤应该是安装、设置默认格式化工具、开启保存时格式化、自定义配置,以及处理常见问题。</think>### 分步配置 VSCode 使用 Prettier 自动保存格式化代码 #### 1. **安装 Prettier 插件** - 在 VSCode 扩展商店中搜索 `Prettier - Code formatter` 并安装[^1][^3]。 #### 2. **设置 Prettier 为默认格式化工具** - 打开 VSCode 设置(快捷键 `Ctrl + ,` 或 `Cmd + ,`)。 - 搜索 `Default Formatter`,选择 `Prettier` 作为默认工具[^3][^4]。 - 验证:右键代码文件选择 `Format Document`,确认使用 Prettier。 #### 3. **开启保存时自动格式化** - 在设置中搜索 `format on save`,勾选复选框启用保存时格式化。 - 效果:修改代码后按 `Ctrl + S` 或 `Cmd + S` 保存时自动格式化。 #### 4. **自定义 Prettier 规则(可选)** - 在项目根目录创建 `.prettierrc` 或 `.prettierrc.json` 文件。 - 示例配置: ```json { "semi": false, "singleQuote": true, "tabWidth": 2 } ``` - 注意:修改配置后需重启 VSCode 以生效[^2]。 #### 5. **处理常见问题** - **其他插件冲突**:若 ESLintVeturPrettier 冲突,安装 `eslint-config-prettier` 或调整 Vetur 设置。 - **文件类型支持**:Prettier 默认支持主流语言(JS/TS/HTML/CSS/JSON 等),其他类型需检查插件兼容性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值