vue3之 eslint、hooks、Prettier的安装与使用(实现代码校验)

vue3之 eslint的安装与使用

  • eslintrc-auto-import.json配置
    • 作用:就是使用了 AutoImport 自动引入了,eslint对此也不会报错
    • .eslintrc.js extends: './.eslintrc-auto-import.json' ],

eslint的安装依赖

  "devDependencies": {
    "eslint": "7.32.0",
    "eslint-config-prettier": "8.3.0",
    "eslint-define-config": "1.2.0",
    "eslint-plugin-import": "2.25.3",
    "eslint-plugin-prettier": "4.0.0",
    "eslint-plugin-vue": "8.1.1",
    "typescript":"4.5.4",
    "@typescript-eslint/eslint-plugin": "5.5.0",
    "@typescript-eslint/parser": "5.5.0"
  }
  • npm i 初始化依赖

.eslintrc.js 的配置

  • eslint的报错:变量未定义时,也就是no-undef 可以再rules之中配置
  • globals: {
    onMounted: true, // 设置为 true 则再组件之中不需要引入 不会标记错误
    },
    在这里插入图片描述
module.exports = {
  root: true,
  env: {
    commonjs: true,
    es6: true,
  },
  globals: {
    onMounted: true, // 设置为 true 则再组件之中不需要引入 不会标记错误
  },
  plugins: ['prettier', '@typescript-eslint', 'import'],
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:vue/vue3-recommended',
    'prettier',
    './.eslintrc-auto-import.json', // 作用:就是读取 .eslintrc-auto-import.json 的配置 因此涉及到的变量也就不会报错
    // './tests/.eslintrc-unit-test.json'
  ],
  // parser: 'vue-eslint-parser',
  // 解析规则
  parserOptions: {
    parser: '@typescript-eslint/parser',
    // parser: "vue-eslint-parser",
    sourceType: 'module',
    // ecmaFeatures: {
    //   jsx: true,
    //   tsx: true
    // }
  },
  rules: {
    // 0不报错, 1代表警告 2代表错误
    // 'no-undef': [2], // 变量未定义 
    //close lf error
    // 'import/no-unresolved': [0],
    // 'vue/multi-word-component-names': 'off',
    // 'vue/no-deprecated-router-link-tag-prop': 'off',
    // 'import/extensions': 'off',
    // 'import/no-absolute-path': 'off',
    // 'no-async-promise-executor': 'off',
    // 'import/no-extraneous-dependencies': 'off',
    // 'vue/no-multiple-template-root': 'off',
    // 'vue/html-self-closing': 'off',
    // 'no-console': 'off',
    // 'no-plusplus': 'off',
    // 'no-useless-escape': 'off',
    // 'no-bitwise': 'off',
    // '@typescript-eslint/no-explicit-any': ['off'],
    // '@typescript-eslint/explicit-module-boundary-types': ['off'],
    // '@typescript-eslint/ban-ts-comment': ['off'],
    // 'vue/no-setup-props-destructure': ['off'],
    // '@typescript-eslint/no-empty-function': ['off'],
    // 'vue/script-setup-uses-vars': ['off'],
    // //can config  to 2 if need more then required
    // '@typescript-eslint/no-unused-vars': [0],
    // 'no-param-reassign': ['off']
  }
}

.eslintignore

public
node_modules
.history
.husky
dist
*.d.ts

配置好这些后 重启vscode ( 需要重启vscode )

ctrl + shift + P
选择 REload Window

App.vue

<template>
  <img alt="Vue logo" src="./assets/logo.png" />
  <HelloWorld msg="Hello Vue 3 + Vite" />
</template>
<script setup>
import HelloWorld from './components/HelloWorld.vue'
import {  } from 'vue'
onMounted(() => {
  console.log('1', import.meta.env) // 获取到环境 相关的配置
})
const a = ref(0)
console.log('aaa', a.value)
useCommon()

</script>

vue3之 git hook的使用

  • package.json
    • “prepare”: “husky install” 脚本指令,需要等依赖安装后,加入,运行( 鼠标 悬浮于 prepare 上方,右键,点击运行脚步 )
  "scripts": {
    "prepare": "husky install"
  },
  "devDependencies": {
    "husky":"7.0.2"
  }
  • npm i
  • 执行 git init 初始化 git
  • 运行 prepare 脚本后,npm run prepare
    • 会产生 .husky 文件目录等文件
      在这里插入图片描述
  • husky / pre-commit ( pre-commit文件作用:就是再git commit之前会执行这个文件 )
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# echo "pre-commit被执行了" -> pre-commit文件作用:就是再git commit之前会执行这个文件

#推送之前运行eslint检查
npm  run lint
##推送之前运行单元测试检查
#npm run test:unit
  • husky / commit-msg
#!/bin/sh
#. "$(dirname "$0")/_/husky.sh"
#在项目中我们会使用commit-msg这个git hook来校验我们commit时添加的备注信息是否符合规范。在以前的我们通常是这样配置:
#--no-install 参数表示强制npx使用项目中node_modules目录中的commitlint包(如果需要开启,注意:需要安装npx)
#npx --no-install commitlint --edit $1
  • eslint 的校验命令
  "scripts": {
    "lint": "eslint --ext .js,.jsx,.vue,.ts,.tsx src --fix"
  },
  • 配置以上之后,再执行 git commit -m "XXX" 会先执行一下 eslint校验代码是否符合,符合可以提交,不符合不给提交
    在这里插入图片描述

vue3 之 Prettier 使用

  • package.json
 "devDependencies": {
    "prettier":"^2.2.1"
  }
  • npm i
  • 目录下新增 .prettierrc 文件
{
  "useTabs": false,
  "tabWidth": 2,
  "printWidth": 120,
  "singleQuote": true,
  "trailingComma": "none",
  "bracketSpacing": true,
  "semi": false,
  "htmlWhitespaceSensitivity": "ignore"
}
  • 注意点:设置默认格式化未 prettier
    在这里插入图片描述
    在这里插入图片描述
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
安装eslintprettier插件来进行vue项目的代码规范和格式化,可以按照以下步骤进行操作: 1. 首先,在项目中安装相关的插件。使用以下命令来安装eslint及其相关插件: ``` npm i eslint@7 -D npm i eslint-config-prettier eslint-plugin-prettier prettier eslint-plugin-vue babel-eslint -D ``` 需要注意的是,这里选择安装eslint7的版本是因为eslint8以上的版本废除了babel-eslint,转而使用@babel/eslint-parser替代。在使用@babel/eslint-parser时,可能会与项目本身的babel版本产生冲突,为了避免改动项目原有的babel版本,可以选择降低eslint版本。 2. 其次,需要在VSCode中安装eslintprettier插件。打开VSCode,通过插件市场搜索并安装"ESLint"和"Prettier - Code formatter"插件。 3. 然后,在项目中进行配置。在项目根目录下创建一个名为".eslintrc.js"的文件,并添加以下配置内容: ```javascript module.exports = { root: true, env: { node: true, }, extends: [ "plugin:vue/essential", "eslint:recommended", "@vue/prettier", ], parserOptions: { parser: "babel-eslint", ecmaVersion: 2020, }, rules: {}, }; ``` 这里使用了"plugin:vue/essential",这是一个预设的规则集,如果想要自己配置规则,可以使用"plugin:vue/essential"预设,并在rules中自行添加规则。 4. 还需要在配置文件中添加一些特殊的配置。在".eslintrc.js"中的parserOptions中添加"parser: 'vue-eslint-parser'",以解决在vue文件中eslint报错的问题。 5. 最后,为了自定义prettier的配置,可以在项目根目录下创建一个名为".prettierrc"的文件,然后在其中添加自定义的prettier配置。例如: ```json { "tabWidth": 2, "useTabs": false, "printWidth": 300, "semi": false, "singleQuote": true, "arrowParens": "avoid", "bracketSpacing": true, "endOfLine": "auto", "eslintIntegration": false, "htmlWhitespaceSensitivity": "ignore", "ignorePath": ".gnore", "trailingComma": "none" } ``` 这里的配置可以根据个人的喜好进行调整,具体的参数意义可以参考Prettier官网的文档。 这样,你就可以在vue项目中安装并配置eslintprettier了。这些工具可以帮助你保持代码规范和格式化,提高代码质量和可读性。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值