下面是项目级完整配置1➡eslint:【吐血分享,博主踩过的坑你跳已往!!跳不外去?太太过了给博主打钱】
浏览器自动打开项目:
你想开释双手吗?你想天天早上打开电脑运行完项目自动在浏览器打开吗?不要9998,不要998,只要你在我们爱的 package.json 中配置一下即可体现。如下图所示:
是的没错,只需添加 "dev": "vite --open", 包你美梦成真。
配置eslint:
是谁以前在vue.config.js内里偷偷的添加lintOnSave: false 关闭eslint检查逃过n节 ,是你吗我的先生女士们。。是我!(扭曲)(爬行)(尖叫)由于eslint太烦了!!
但是eslint可以资助我们检查js语法及少部门格式问题。 自从和我的同事(他有代码洁癖)一起工作之后,我发现我被迫改了很多…非常感谢你,和我一起练习的同事,由于有你,代码更规范~~~
eslint官网直达路线
安装eslint:
安装最新版本的eslint:
告诉eslint,你要怎么工作:
我们用eslint 检查语法、发现错误,如下图选择:
我们的项目采用的js模块 的模块式开发,如下图:
框架选取vue,如下图:
是否使用ts?是的。如下图:
项目运行在浏览器还是node上,我们选择浏览器。如下图:
使用eslint去校验我们的vue、ts语法,需要安装它提示的三个插件,是否选择安装呢?是的。如下图:
使用什么包管理工具下载呢?我这里使用的是pnpm,根据自己选择哈。如下图:
安装乐成如下图(安装乐成会自动生成eslint.config.js):
安装其他版本的eslint:
but 我这次需要安装v8.x的 制止我的一些版本冲突问题哈,根据自己的需求选择。
- pnpm install eslint@8.57.1 --save-dev
复制代码
安装插件指令供配置:
- pnpm install -D eslint-plugin-import eslint-plugin-vue eslint-plugin-node eslint-plugin-prettier eslint-config-prettier eslint-plugin-node @babel/eslint-parser
复制代码 在根目录下创建 .eslintrc.cjs 文件,并配置它:
- // @see: http://eslint.cn
- module.exports = {
- root: true,
- env: {
- browser: true,
- node: true,
- es6: true
- },
- // 指定如何解析语法
- parser: "vue-eslint-parser",
- // 优先级低于 parse 的语法解析配置
- parserOptions: {
- parser: "@typescript-eslint/parser",
- ecmaVersion: 2020,
- sourceType: "module",
- jsxPragma: "React",
- ecmaFeatures: {
- jsx: true
- }
- },
- // 继承某些已有的规则
- extends: ["plugin:vue/vue3-recommended", "plugin:@typescript-eslint/recommended", "plugin:prettier/recommended"],
- /**
- * "off" 或 0 ==> 关闭规则
- * "warn" 或 1 ==> 打开的规则作为警告(不影响代码执行)
- * "error" 或 2 ==> 规则作为一个错误(代码不能执行,界面报错)
- */
- rules: {
- // eslint (http://eslint.cn/docs/rules)
- "no-var": "error", // 要求使用 let 或 const 而不是 var
- "no-multiple-empty-lines": ["error", { max: 1 }], // 不允许多个空行
- "prefer-const": "off", // 使用 let 关键字声明但在初始分配后从未重新分配的变量,要求使用 const
- "no-use-before-define": "off", // 禁止在 函数/类/变量 定义之前使用它们
- // typeScript (https://typescript-eslint.io/rules)
- "@typescript-eslint/no-unused-vars": "error", // 禁止定义未使用的变量
- "@typescript-eslint/no-empty-function": "error", // 禁止空函数
- "@typescript-eslint/prefer-ts-expect-error": "error", // 禁止使用 @ts-ignore
- "@typescript-eslint/ban-ts-comment": "error", // 禁止 @ts-<directive> 使用注释或要求在指令后进行描述
- "@typescript-eslint/no-inferrable-types": "off", // 可以轻松推断的显式类型可能会增加不必要的冗长
- "@typescript-eslint/no-namespace": "off", // 禁止使用自定义 TypeScript 模块和命名空间
- "@typescript-eslint/no-explicit-any": "off", // 禁止使用 any 类型
- "@typescript-eslint/ban-types": "off", // 禁止使用特定类型
- "@typescript-eslint/no-var-requires": "off", // 允许使用 require() 函数导入模块
- "@typescript-eslint/no-non-null-assertion": "off", // 不允许使用后缀运算符的非空断言(!)
- "@typescript-eslint/no-require-imports": "off",
- // vue (https://eslint.vuejs.org/rules)
- "vue/script-setup-uses-vars": "error", // 防止<script setup>使用的变量<template>被标记为未使用,此规则仅在启用该 no-unused-vars 规则时有效
- "vue/v-slot-style": "error", // 强制执行 v-slot 指令样式
- "vue/no-mutating-props": "error", // 不允许改变组件 prop
- "vue/custom-event-name-casing": "error", // 为自定义事件名称强制使用特定大小写
- "vue/html-closing-bracket-newline": "error", // 在标签的右括号之前要求或禁止换行
- "vue/attribute-hyphenation": "error", // 对模板中的自定义组件强制执行属性命名样式:my-prop="prop"
- "vue/attributes-order": "off", // vue api使用顺序,强制执行属性顺序
- "vue/no-v-html": "off", // 禁止使用 v-html
- "vue/require-default-prop": "off", // 此规则要求为每个 prop 为必填时,必须提供默认值
- "vue/multi-word-component-names": "off", // 要求组件名称始终为 “-” 链接的单词
- "vue/no-setup-props-destructure": "off" // 禁止解构 props 传递给 setup
- }
- };
复制代码 根目录创建.eslintignore文件:
- *.sh
- node_modules
- *.md
- *.woff
- *.ttf
- .vscode
- .idea
- dist
- /public
- /docs
- .husky
- .local
- /bin
- /src/mock/*
- stats.html
复制代码 在package.json文件中添加如下代码:
- "lint": "eslint src",
- "fix": "eslint src --fix",
复制代码
测试一下eslint的效果:
fine!
自动修补了,有点意思啊eslint你小子
so,在运行前甚至都可以先 pnpm run lint --fix 了 (★ ω ★)
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |