news 2026/7/4 9:56:21

Vue3-Eslint配合prettier完成代码风格配置

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Vue3-Eslint配合prettier完成代码风格配置

前提:创建项目时勾选了prettier和eslint

若未,请参考Eslint:已有vue2项目添加eslint自动格式化,Eslint (standard)+ Husky + Lint-staged+prettier_vue2 eslint-CSDN博客

prettier风格配置

官网:https://prettier.io

Eslint:代码纠错,关注于规范

prettier:专注于代码格式化的插件,让代码更加美观

两者各有所长,配合使用优化代码

生效前提:

1)禁用格式化插件prettier

使用的是内置的prettier包,可以在package中查看版本

没有就下载>>pnpm add --save-dev prettier

2)安装Eslint插件

3)打开vscode的设置,点击右上角的页面进入代码设置

配置保存时自动修复以及关闭format on save

//ESlint插件+Vscode配置实现自动格式化修复 "editor.codeActionsOnSave": { "source.fixAll": "explicit" }, //关闭保存自动格式化 "editor.formatOnSave": false

按照红色框内修改代码

在配置文件.eslintrc.cjs中添加如下代码:查看注释理解代码含义,更多规则查看prettier官网

rules: { 'prettier/prettier': [ 'warn', { singleQuote: true, //单引号 semi: false, //无分号 printWidth: 80, //每行宽度至多80字符 trailingComma: 'none', //不加对象|数组最后逗号 endOfLine: 'auto' //换行符号不限制(win mac 不一致) } ] }

vue组件名称多单词组成(忽略index.vue)

vue组件名称要求必须由多单词组成,但是有时候需要设置index.vue就会报错,所以设置以下代码可以让index.vue不报错

'vue/multi-word-component-names': [ 'warn', { ignores: ['index'] //vue组件名称多单词组成(忽略index.vue) } ]

props解构(关闭)

正常props解构就会导致数据响应式丢失,但是我们可以有方法让其响应式不丢失,所以这里可以设置代码使props解构时不报错

'vue/no-setup-props-destructrue': ['off'], //关闭props解构的校验(props解构丢失响应式)

未定义的变量使用时报错

'no-undef': 'error' //添加未定义变量错误提示,create-vue@3.6.3关闭,这里加上是为了支持下一个章节演示

转义字符校验(关闭)

错误:Unnecessary escape character:\x.

解决: 在.eslintrc.cjs文件中的rules添加'no-useless-escape': 'off'

总结:

.prettierrc.json文件

{ "$schema": "https://json.schemastore.org/prettierrc", "semi": false, "singleQuote": true, "printWidth": 80, "trailingComma": "none", "endOfLine": "auto" }

eslint.config.js

import { defineConfig, globalIgnores } from 'eslint/config' import globals from 'globals' import js from '@eslint/js' import pluginVue from 'eslint-plugin-vue' import pluginOxlint from 'eslint-plugin-oxlint' import eslintPluginPrettier from 'eslint-plugin-prettier' import skipFormatting from 'eslint-config-prettier/flat' export default defineConfig([ { name: 'app/files-to-lint', files: ['**/*.{vue,js,mjs,jsx}'] }, globalIgnores(['**/dist/**', '**/dist-ssr/**', '**/coverage/**']), { languageOptions: { globals: { ...globals.browser } } }, js.configs.recommended, ...pluginVue.configs['flat/essential'], ...pluginOxlint.buildFromOxlintConfigFile('.oxlintrc.json'), { plugins: { prettier: eslintPluginPrettier }, rules: { 'prettier/prettier': 'warn', 'vue/multi-word-component-names': ['warn', { ignores: ['index'] }], 'vue/no-setup-props-destructure': 'off', 'no-undef': 'error' }, languageOptions: { globals: { ...globals.browser, ...globals.node, ElMessage: 'readonly', ElMessageBox: 'readonly', ElLoading: 'readonly' } } }, skipFormatting ])

补充:如果是用的TS文件,文件名就是eslint.config.ts

先引入eslint-plugin-prettier

npm i -D eslint-plugin-prettier pnpm add -D globals eslint-plugin-prettier
import { globalIgnores } from 'eslint/config' import { defineConfigWithVueTs, vueTsConfigs } from '@vue/eslint-config-typescript' import pluginVue from 'eslint-plugin-vue' import pluginVitest from '@vitest/eslint-plugin' import pluginOxlint from 'eslint-plugin-oxlint' import eslintPluginPrettier from 'eslint-plugin-prettier' import skipFormatting from 'eslint-config-prettier/flat' import globals from 'globals' // To allow more languages other than `ts` in `.vue` files, uncomment the following lines: // import { configureVueProject } from '@vue/eslint-config-typescript' // configureVueProject({ scriptLangs: ['ts', 'tsx'] }) // More info at https://github.com/vuejs/eslint-config-typescript/#advanced-setup export default defineConfigWithVueTs( { name: 'app/files-to-lint', files: ['**/*.{vue,ts,mts,tsx}'] }, globalIgnores(['**/dist/**', '**/dist-ssr/**', '**/coverage/**']), ...pluginVue.configs['flat/essential'], vueTsConfigs.recommended, { ...pluginVitest.configs.recommended, files: ['src/**/__tests__/*'] }, ...pluginOxlint.buildFromOxlintConfigFile('.oxlintrc.json'), { plugins: { prettier: eslintPluginPrettier }, rules: { 'prettier/prettier': [ 'warn', { singleQuote: true, semi: false, printWidth: 80, trailingComma: 'none', endOfLine: 'auto' } ], 'vue/multi-word-component-names': ['warn', { ignores: ['index'] }], 'vue/no-setup-props-destructure': 'off', 'no-undef': 'error' }, languageOptions: { globals: { ...globals.browser, ...globals.node, ElMessage: 'readonly', ElMessageBox: 'readonly', ElLoading: 'readonly' } } }, skipFormatting )

有时候会出现校验和保存冲突的情况,用下面的代码消除某一个文件的校验

npx eslint 文件名 --fix
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/4 9:56:27

AppShark静态污点分析:Android应用安全深度检测实战指南

1. 项目概述:为什么我们需要AppShark这样的工具?在Android应用开发与安全评估的日常工作中,我们常常面临一个核心矛盾:应用的功能迭代越来越快,但安全漏洞的发现和修复却往往滞后。传统的动态测试(如Fuzzin…

作者头像 李华
网站建设 2026/7/4 9:53:12

Dify大模型接入实战:从云端API到本地部署的完整指南

你刚把 Dify 部署好,看着清爽的界面,心想:“这下好了,可以大展拳脚了。” 但当你兴冲冲地准备接入第一个大模型时,却发现事情没那么简单。是直接填个 API Key 就行了吗?为什么我的本地模型连不上&#xff1…

作者头像 李华
网站建设 2026/7/4 9:53:02

三步搞定跨语言障碍:STranslate翻译工具完全指南

三步搞定跨语言障碍:STranslate翻译工具完全指南 【免费下载链接】STranslate A ready-to-go translation ocr tool developed with WPF/WPF 开发的一款即用即走的翻译、OCR工具 项目地址: https://gitcode.com/gh_mirrors/st/STranslate 还在为看不懂外文文…

作者头像 李华
网站建设 2026/7/4 9:53:05

AI 学习路径推荐:别把薄弱点变成焦虑清单

AI 学习路径推荐:别把薄弱点变成焦虑清单 刷题久了,很容易发现自己哪里都薄弱:DP 不稳,图论害怕,字符串忘模板,二分边界老写错。AI 学习路径推荐如果只列一堆薄弱点,很快就变成焦虑清单。真正有…

作者头像 李华
网站建设 2026/7/4 9:52:46

Vanna 2.0企业级自然语言SQL生成架构解析与生产环境部署实战

Vanna 2.0企业级自然语言SQL生成架构解析与生产环境部署实战 【免费下载链接】vanna 🤖 Chat with your SQL database 📊. Accurate Text-to-SQL Generation via LLMs using Agentic Retrieval 🔄. 项目地址: https://gitcode.com/GitHub_T…

作者头像 李华
网站建设 2026/7/4 9:52:00

Beep-Beep用户端界面设计:从UI组件到完整交互流程详解

Beep-Beep用户端界面设计:从UI组件到完整交互流程详解 【免费下载链接】beep-beep 项目地址: https://gitcode.com/gh_mirrors/be/beep-beep Beep-Beep是一个基于Kotlin Compose Multiplatform构建的全栈开源项目,专注于外卖配送和出租车叫车服务…

作者头像 李华