報(bào)錯(cuò)一:
VUE3 You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignor
8:1 error Delete `?` prettier/prettier
? 1 problem (1 error, 0 warnings)
1 error and 0 warnings potentially fixable with the `--fix` option
原因:與創(chuàng)建項(xiàng)目時(shí)選擇的 eslint 的設(shè)置問題,可以通過“—fix”選項(xiàng)修復(fù)
.解決方法:
package.json
//原代碼
"scripts": {
...
"lint": "vue-cli-service lint"
},
更改:
"scripts": {
...
"lint": "eslint --fix --ext .js,.vue src"
},
報(bào)錯(cuò)二:
提示建議我們使用特殊注釋禁用某些警告。使用//eslint disable next line忽略下一行。使用/eslint disable/忽略文件中的所有警告。
You may use special comments to disable some warnings.
Use // eslint-disable-next-line to ignore the next line.
Use /* eslint-disable */ to ignore all warnings in a file.
解決辦法:
我們?cè)?.eslintrc.js 里面注釋掉?plugin:prettier/recommended?就可以了
'extends': [
// 'plugin:vue/vue3-essential',
'eslint:recommended',
'@vue/typescript/recommended'
//'plugin:prettier/recommended'
],
報(bào)錯(cuò)三:
這個(gè)報(bào)錯(cuò)是建議我們使用?駝峰命名
6:9 ?error ?Component name "My" should always be multi-word ?vue/multi-word-component-names
解決辦法:
① 按照規(guī)則走,改駝峰命名
②但是像以上我就一個(gè) My ,這樣要寫個(gè)駝峰不是很合理,所以可以在 .eslintrc.js 文件寫一條規(guī)則:
rules: {
"no-console": process.env.NODE_ENV === "production" ? "warn" : "off",
"no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off",
// 關(guān)閉駝峰命名規(guī)則
'vue/multi-word-component-names': 0,
},
報(bào)錯(cuò)四:
Error-Do not use “// @ts-ignore“ because it alters compilation errors問題的處理
Error-Do not use “// @ts-ignore“ because it alters compilation errors問題的處理
使用TS編寫代碼時(shí),有些情況下,比如第三方的庫對(duì)象,我們想增加一些屬性,并且確認(rèn)是沒問題的,但是TS檢查時(shí)會(huì)報(bào)錯(cuò)導(dǎo)致不能正常編譯運(yùn)行
解決辦法:
我們通過添加// @ts-ignore來告訴TS該條語句不檢查類型問題,此時(shí)是可以正常編譯了,但是// @ts-ignore這條注釋標(biāo)紅了很難受:
這個(gè)我們可以通過修改.eslintrc.js
文件來消除該提示:
module.exports = {
...
rules: {
...
"@typescript-eslint/ban-ts-comment": "off",
}
}
報(bào)錯(cuò)五:使用Vue3 Script Setup時(shí) ESLint 報(bào)錯(cuò) ‘defineProps‘ is not defined
Vue 3 的 Script Setup 語法引入了 defineProps、defineEmits、defineExpose、withDefaults 的編譯器宏。然而某些情況下,ESLint 會(huì)報(bào)錯(cuò)以上編譯器宏函數(shù)未定義。
本文將介紹兩種解決方案來解決這個(gè)問題(假定你的項(xiàng)目使用 Vue-Cli 進(jìn)行初始化)。
Step 1. 檢查 eslint-plugin-vue 的版本
npm list eslint-plugin-vue
若版本在 v8.0.0 以上,跳轉(zhuǎn)到 Step 2,否則直接到 Step 3 的內(nèi)容。
Step 2. 版本為 v8.0.0+
打開 .eslintrc.js 文件并修改如下:
env: {
node: true,
// The Follow config only works with eslint-plugin-vue v8.0.0+
"vue/setup-compiler-macros": true,
},
Step 3. 版本為 v8.0.0 以下
打開 .eslintrc.js 文件并修改如下:
// The Follow configs works with eslint-plugin-vue v7.x.x
globals: {
defineProps: "readonly",
defineEmits: "readonly",
defineExpose: "readonly",
withDefaults: "readonly",
},
?更多的Eslint編寫參考如下:
module.exports = {
parser: '@typescript-eslint/parser',
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'plugin:prettier/recommended'
],
env: {
browser: true,
es2021: true
},
extends: ['eslint:recommended'],
plugins: ['@typescript-eslint', 'prettier'],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module'
},
rules: {
'prettier/prettier': 'error',
'no-extra-semi': 'off',
'@typescript-eslint/camelcase': 'off',
'@typescript-eslint/ban-ts-ignore': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-extra-semi': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-empty-interface': 'off'
}
}
源頭解決(不推薦)
Vue 項(xiàng)目報(bào)錯(cuò):‘XXXXX‘ is not defined ( no-undef ) 解決方法
問題描識(shí):使用vue的時(shí)候,使用一個(gè)全局變量或在當(dāng)前方法調(diào)用別的方法,ESLint的語法會(huì)出現(xiàn)ESLint: ‘Aliplayer’ is not defined. (no-undef),說未定義,這時(shí)我們可以添加配置,取消這個(gè)校驗(yàn)。
在node_modules文件夾下面的eslint文件夾下面的conf里面的eslint-recommended.js文件注釋掉"no-undef": “error”,這行代碼
node_modules
? ? ? ? --eslint
? ? ? ? ? ? ? ? --conf
? ? ? ? ? ? ? ? ? ? ? ? --eslint-recommended.js
文章來源:http://www.zghlxwxcb.cn/news/detail-478522.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-478522.html
到了這里,關(guān)于vue3-webpack遇到Eslint各種報(bào)錯(cuò) Vue 項(xiàng)目報(bào)錯(cuò):‘XXXXX‘ is not defined ( no-undef ) 解決方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!