vue?eslint報錯:Component?name?“xxxxx“?should?always?be?multi-word.eslintvue的4種解決方案
報錯代碼
vue-cli全新創(chuàng)建項目,并建立組件時提示報錯,報錯如下:
vscode標紅提示:
Component name "index" should always be multi-word.eslintvue/multi-word-component-names
npm run serve / yarn serve報錯:
ERROR Failed to compile with 1 error 下午6:02:08
C:\Users\wally\Desktop\vscode\vue\seal\seal_web\src\views\home\index.vue
1:1 error Component name "index" should always be multi-word vue/multi-word-component-names? 1 problem (1 error, 0 warnings)
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.
ERROR in
C:\Users\wally\Desktop\vscode\vue\seal\seal_web\src\views\home\index.vue
1:1 error Component name "index" should always be multi-word vue/multi-word-component-names? 1 problem (1 error, 0 warnings)
webpack compiled with 1 error
原因
新手在組件命名的時候不夠規(guī)范,根據(jù)官方風格指南,除了根組件(App.vue)外,自定義組件名稱應該由多單詞組成,防止和html標簽沖突。
而最新的vue-cli創(chuàng)建的項目使用了最新的vue/cli-plugin-eslint插件,在vue/cli-plugin-eslint v7.20.0版本之后就引用了vue/multi-word-component-names規(guī)則,所以在編譯的時候判定此次錯誤。
解決方案
方案一
改名
修改組件名為多個單詞,使用大駝峰命名方式或者用“-”連接單詞。但是有時候因為個別原因不能改名,此方案不好使,看下面兩個方案。
方案二:
關(guān)閉校驗
在根目錄下找到vue.config.js文件(如果沒有則新建一個),添加下面的代碼
lintOnSave: false
添加后文件示例:
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
transpileDependencies: true,
//關(guān)閉eslint校驗
lintOnSave: false
})
此方案治標不治本,只是編譯時不報錯,如果使用vscode+eslint 會在文件頭標紅提示,強迫癥根本忍受不了,并且官方并不建議直接關(guān)閉校驗,所以推薦使用方案三
方案三(推薦)
關(guān)閉命名規(guī)則校驗
在根目錄下找到 .eslintrc.js 文件,同樣如果沒有則新建一個(注意文件前有個點),代碼如下
添加一行:
"vue/multi-word-component-names":"off",
文件內(nèi)容:
module.exports = {
root: true,
env: {
node: true
},
'extends': [
'plugin:vue/essential',
'eslint:recommended'
],
parserOptions: {
parser: '@babel/eslint-parser'
},
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
//在rules中添加自定義規(guī)則
//關(guān)閉組件命名規(guī)則
"vue/multi-word-component-names":"off",
},
overrides: [
{
files: [
'**/__tests__/*.{j,t}s?(x)',
'**/tests/unit/**/*.spec.{j,t}s?(x)'
],
env: {
jest: true
}
}
]
}以上是關(guān)閉命名規(guī)則,將不會校驗組件名,官方建議設置是根據(jù)組件名進行忽略
忽略個別組件名
// 添加組件命名忽略規(guī)則
"vue/multi-word-component-names": ["error",{
"ignores": ["index"]//需要忽略的組件名
}]
方案四(推薦):
方案三是關(guān)閉和忽略組件名規(guī)則,但是有時候還是需要團隊有個共同規(guī)范,不能關(guān)閉,同時文件名可能和組件名不一致時,例如我需要每個頁面入口為index.vue,但是組件名為MyHome,用忽略組件名的方式可能需要同時添加index和MyHome,就顯得很傻瓜?;蛘呶倚枰酚山M件忽略,非路由組件不忽略,那如何在這種情況下修改規(guī)則更好用呢?因此我找到了第四種方式。方案三是根據(jù)組件名忽略,此方案是根據(jù)文件進行關(guān)閉規(guī)則,更適用。
關(guān)閉某文件命名規(guī)則校驗
在根目錄下找到 .eslintrc.js 文件,同樣如果沒有則新建一個(注意文件前有個點),代碼如下
在文件的overrides中添加如下代碼:
{
files: ['src/views/index.vue','src/views/**/index.vue'], // 匹配views和二級目錄中的index.vue
rules: {
'vue/multi-word-component-names':"off",
} //給上面匹配的文件指定規(guī)則
}
其中的 files: [] 是用于匹配文件的,*號代表所有文件。index.vue也可以改成 *.vue,這就是匹配目錄下的所有vue文件
文件內(nèi)容:
module.exports = {
root: true,
env: {
node: true
},
'extends': [
'plugin:vue/essential',
'eslint:recommended'
],
parserOptions: {
parser: '@babel/eslint-parser'
},
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
},
overrides: [
//這里是添加的代碼
{
files: ['src/views/index.vue','src/views/**/index.vue'], // 匹配views和二級目錄中的index.vue
rules: {
'vue/multi-word-component-names':"off",
} //給上面匹配的文件指定規(guī)則
},
{
files: [
'**/__tests__/*.{j,t}s?(x)',
'**/tests/unit/**/*.spec.{j,t}s?(x)'
],
env: {
jest: true
}
}
]
}其實和方案三基本一致,只是放的位置不同
總結(jié)
到此這篇關(guān)于vue eslint報錯:Component name “xxxxx“ should always be multi-word.eslintvue的4種解決方案的文章就介紹到這了,更多相關(guān)vue eslint報錯解決內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue點擊Dashboard不同內(nèi)容 跳轉(zhuǎn)到同一表格的實例
這篇文章主要介紹了vue點擊Dashboard不同內(nèi)容 跳轉(zhuǎn)到同一表格的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
vue項目中做編輯功能傳遞數(shù)據(jù)時遇到問題的解決方法
這篇文章主要介紹了vue項目中做編輯功能傳遞數(shù)據(jù)時遇到問題的解決方法,vue父組件向子組件傳遞數(shù)據(jù)的問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
解決$store.getters調(diào)用不執(zhí)行的問題
今天小編就為大家分享一篇解決$store.getters調(diào)用不執(zhí)行的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11

