当前位置: 首页 > news >正文

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
http://www.cnnetsun.cn/news/3134309.html

相关文章:

  • AppShark静态污点分析:Android应用安全深度检测实战指南
  • Dify大模型接入实战:从云端API到本地部署的完整指南
  • 三步搞定跨语言障碍:STranslate翻译工具完全指南
  • AI 学习路径推荐:别把薄弱点变成焦虑清单
  • Vanna 2.0企业级自然语言SQL生成架构解析与生产环境部署实战
  • Beep-Beep用户端界面设计:从UI组件到完整交互流程详解
  • Vendure插件系统完全指南:现代无头电商架构的扩展核心
  • 告别硬盘混乱:12个Krokiet工具让你轻松找回50GB空间
  • Crucible与LLVM集成教程:构建C/C++程序的符号验证流程
  • tools.cli高级技巧:如何优雅处理复杂命令行参数与子命令
  • MZmine 3终极指南:如何免费快速处理质谱数据的完整解决方案
  • 计算机毕业设计之jsp浪淘音乐网站的设计与实现
  • 炉石传说终极增强插件:HsMod 55个功能完整指南与快速配置教程
  • 【免费下载】 E-Hentai-Downloader:一键下载E-Hentai图库的利器
  • 解锁iOS设备潜能:palera1n越狱工具完整指南
  • Crucible高级技巧:提升符号模拟效率的10个实用方法
  • 从源码到二进制:揭秘readpe的libpe核心库设计与实现原理
  • AES-CBC数据解密实战:独立密钥、IV与跨平台对接全解析
  • HsMod终极指南:如何用BepInEx框架打造个性化炉石传说体验
  • 如何让AI告别平庸设计:Taste-Skill完整使用指南与实战技巧
  • 终极Blender资源大全:200+免费插件与素材库完整指南
  • 5步构建智能金融交易大脑:TradingAgents多智能体框架实战指南
  • Ornith-1.0-9B-MTP-GGUF量化版本对比:Q4_K_M、Q8_0还是IQ系列?哪款最适合你的硬件
  • ZFS-inplace-rebalancing安全使用指南:避免数据丢失的关键步骤
  • HsMod:基于BepInEx框架的炉石传说功能增强插件技术解析
  • 免费歌词批量获取工具:3分钟搞定网易云QQ音乐歌词整理
  • Windows Research Kernel (WRK) 与Linux内核对比:两大操作系统内核设计的差异分析
  • 运动增肌学习笔记
  • Touch WX与H5无缝转换:一套代码开发两个平台应用的秘诀
  • Ornith-1.0-9B-MTP-GGUF新手入门:从下载到部署的完整步骤