前言
本文主要记录下 vue Eslint 报错 error Component name "index" should always be multi-word vue/multi-word-component-names 的解决方案。
一、报错原因
使用最新 vue-cli 创建项目,npm run serve 运行项目时,报错如下图。
原因是 eslint-plugin-vue 版本更新了,相较之前版本,@8 版本中新增了不少规则,第一条就是 'vue/multi-word-component-names': 'error', 要求组件名称以驼峰格式命名,所以 index.vue 会报错。
二、解决方案
- 按照规则,使用驼峰命名,例如 AppHeader.vue
- 在 .eslintrc.js 文件中关闭命名规则
// .eslintrc.js
module.exports = {
root: true,
env: {
node: true,
},
extends: [
"plugin:vue/vue3-essential",
"eslint:recommended",
"@vue/typescript/recommended",
"plugin:prettier/recommended",
],
parserOptions: {
ecmaVersion: 2020,
},
rules: {
"vue/multi-word-component-names": [
"error",
{
ignores: ["index"], //需要忽略的组件名
},
],
},
};