vue中配置Eslint的步驟代碼解析
更新時間:2025年07月15日 10:04:53 作者:白桃與貓
本文介紹Vue項目中配置ESLint和Prettier的步驟,包括安裝依賴、創(chuàng)建配置文件、設(shè)置package.json腳本、格式化配置及運(yùn)行檢查,實現(xiàn)代碼規(guī)范,感興趣的朋友一起看看吧
1. 安裝ESLint相關(guān)依賴
# 安裝ESLint核心包 npm install --save-dev eslint # 安裝Vue相關(guān)插件 npm install --save-dev eslint-plugin-vue vue-eslint-parser # 安裝JavaScript推薦配置 npm install --save-dev @eslint/js globals # 安裝Prettier相關(guān)(用于代碼格式化) npm install --save-dev prettier eslint-plugin-prettier
2. 創(chuàng)建ESLint配置文件
import globals from 'globals';
import pluginJs from '@eslint/js';
import pluginVue from 'eslint-plugin-vue';
import vueParser from 'vue-eslint-parser';
import prettier from 'eslint-plugin-prettier';
export default [
// 全局忽略配置
{
ignores: [
'**/*.config.js',
'dist/**',
'node_modules/**',
'!**/eslint.config.js',
],
},
// JavaScript文件配置
{
files: ['**/*.js', '**/*.mjs'],//針對.js和.mjs文件
languageOptions: {
globals: globals.browser,
ecmaVersion: 'latest',
sourceType: 'module',
},
plugins: {
prettier,
},
rules: {
'prettier/prettier': 'error',
'no-var': 'error',// 禁止使用var
'no-multiple-empty-lines': ['warn', { max: 1 }],//最多允許1個空行
'no-console': 'warn',//禁止使用console
'no-debugger': 'warn',//禁止使用debugger
'no-unexpected-multiline': 'error',//防止意外的多行語句
'no-useless-escape': 'off',//關(guān)閉對不必要轉(zhuǎn)義字符的檢查
'no-unused-vars': 'error',//禁止未使用的變量
},
},
// Vue文件配置
{
files: ['**/*.vue'],
languageOptions: {
globals: globals.browser,
parser: vueParser,
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
ecmaFeatures: {
jsx: true,
},
},
},
plugins: {
prettier,
vue: pluginVue,
},
rules: {
'prettier/prettier': 'error',
'no-var': 'error',
'no-multiple-empty-lines': ['warn', { max: 1 }],
'no-console': 'warn',
'no-debugger': 'warn',
'no-unexpected-multiline': 'error',
'no-useless-escape': 'off',
'no-unused-vars': 'error',
// Vue特定規(guī)則
'vue/multi-word-component-names': 'off',//關(guān)閉組件名必須時多詞的要求
'vue/script-setup-uses-vars': 'error',//確保<script setup>中變量被正確使用
'vue/no-mutating-props': 'off',
'vue/attribute-hyphenation': 'off',
'vue/valid-v-slot': [
'error',
{
allowModifiers: true,
},
],
},
},
// 推薦配置
pluginJs.configs.recommended,//將使用區(qū)推薦的有關(guān)JavaScript的ESLint規(guī)則
...pluginVue.configs['flat/essential'],// 使用Vue.js的基本ESLint規(guī)則
];3. 配置package.json腳本
{
"scripts": {
"lint": "eslint . --fix",
"lint:check": "eslint .",
"format": "prettier --write ."
}
}4. 創(chuàng)建prettier配置文件
對代碼進(jìn)行格式化配置
{
"semi": true,// 在每個語句的末尾添加分號
"singleQuote": true,// 使用單引號
"tabWidth": 2,// 設(shè)置縮進(jìn)的空格數(shù)為2
"useTabs": false,// 使用空格縮進(jìn)
"printWidth": 100,//每行的最大字符數(shù)為100
"trailingComma": "all"http://在多行結(jié)構(gòu)的最后一個元素后添加逗號
}5. 運(yùn)行后檢查
# 檢查代碼 npm run lint:check # 檢查并自動修復(fù) npm run lint # 格式化代碼 npm run format
到此這篇關(guān)于vue中配置Eslint的步驟的文章就介紹到這了,更多相關(guān)vue配置Eslint內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue 實現(xiàn)高級穿梭框 Transfer 封裝過程
本文介紹了基于Vue2和Element-UI實現(xiàn)的高級穿梭框組件Transfer的設(shè)計與技術(shù)方案,組件支持多項選擇,并能實時同步已選擇項,包含豎版和橫版設(shè)計稿,并提供了組件的使用方法和源碼,此組件具備本地分頁和搜索功能,適用于需要在兩個列表間進(jìn)行數(shù)據(jù)選擇和同步的場景2024-09-09
Vue+express+Socket實現(xiàn)聊天功能
這篇文章主要為大家詳細(xì)介紹了Vue+express+Socket實現(xiàn)聊天功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
ElementPlus的el-upload組件使用方式(圖片上傳)
文章詳細(xì)介紹了ElementPlus的el-upload組件,涵蓋了基本用法、關(guān)鍵屬性、文件限制、顯示設(shè)置以及常見問題的解決方案,通過示例和最佳實踐建議,幫助開發(fā)者實現(xiàn)靈活的圖片上傳功能2026-01-01
vue3組合式api實現(xiàn)v-lazy圖片懶加載的方法實例
vue作為前端主流的3大框架之一,目前在國內(nèi)有著非常廣泛的應(yīng)用,下面這篇文章主要給大家介紹了關(guān)于vue3組合式api實現(xiàn)v-lazy圖片懶加載的相關(guān)資料,需要的朋友可以參考下2022-09-09

