Vue typescript项目配置eslint+prettier

前言

本文基于

  • "vite": "^5.0.0"

1.安装依赖

安装 eslint

javascript 复制代码
yarn add eslint --dev

安装 eslint-plugin-vue

javascript 复制代码
yarn add eslint-plugin-vue --dev

主要用于检查 Vue 文件语法

安装 prettier 及相关插件

javascript 复制代码
yarn add prettier eslint-config-prettier eslint-plugin-prettier --dev

安装 typescript 解析器、规则补充

javascript 复制代码
yarn add @typescript-eslint/parser @typescript-eslint/eslint-plugin --dev

2.根目录创建 .eslintrc.cjs

javascript 复制代码
module.exports = {
  env: { browser: true, es2020: true },
  extends: [
    'eslint:recommended',
    'plugin:vue/vue3-recommended',
    'plugin:@typescript-eslint/recommended',
    'eslint-config-prettier',
    'plugin:prettier/recommended'
  ],
  parser: 'vue-eslint-parser',
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module',
    parser: '@typescript-eslint/parser'
  },
  plugins: ['vue', 'prettier'],
  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
  }
};

3.根目录创建 .prettierrc.cjs

javascript 复制代码
module.exports = {
  // 字符串是否使用单引号
  singleQuote: true,
  // 大括号首尾是否需要空格
  bracketSpacing: true,
  // 对象末尾是否需要逗号
  trailingComma: 'none',
  // 箭头函数参数括号(1个参数不需要, 1个以上需要)
  arrowParens: 'avoid',
  // 折行标准(默认)
  proseWrap: 'preserve',
  // 根据显示样式决定html是否折行
  htmlWhitespaceSensitivity: 'css',
  // 换行符(crlf/lf/auto)
  endOfLine: 'auto'
};

4.配置 package.json 的 scripts 字段

javascript 复制代码
"scripts": {
   ...
   "lint": "eslint . --ext vue,ts --report-unused-disable-directives --max-warnings 0"
}

5.测试配置

javascript 复制代码
yarn lint

如果本篇文章对你有帮助的话,很高兴能够帮助上你。

当然,如果你觉得文章有什么让你觉得不合理、或者有更简单的实现方法又或者有理解不来的地方,希望你在看到之后能够在评论里指出来,我会在看到之后尽快的回复你。

相关推荐
:mnong7 小时前
附图报价系统设计分析5
electron·pdf·vue·cad·dwg·定额
烛衔溟12 小时前
TypeScript 接口继承与混合类型
linux·ubuntu·typescript
送鱼的老默12 小时前
学习笔记--入门typescript直接案例开搞
前端·typescript
桃花键神16 小时前
【2026精品项目】基于SpringBoot3+Vue3的旧物置换系统(包含源码+项目文档+SQL脚本+部署教程)
数据库·spring boot·sql·vue
spmcor19 小时前
TypeScript 中 null 与 undefined 的区别 —— 一篇彻底搞懂的指南
typescript
烛衔溟20 小时前
TypeScript 接口实战 —— 处理复杂嵌套对象
linux·ubuntu·typescript
FlyWIHTSKY21 小时前
**Vue 3 `<script setup>` 语法糖** 中的一行解构赋值,用来**从父组件透传下来的属性(attrs)
vue
CAE虚拟与现实21 小时前
前后端调试常用工具大全
前端·后端·vue·react·angular
web行路人2 天前
前端对Commands(斜杠命令)一些常用
前端·javascript·vue.js·vue
求学中--2 天前
ArkUI电商首页完整实战
华为·typescript·harmonyos