Component name "index" should always be multi-word vue/multi-word-component-nam

659 阅读1分钟

原因是 eslint-plugin-vue 版本更新了,相较之前版本,@8 版本中新增了不少规则, 第一条就是 'vue/multi-word-component-names': 'error', 要求组件名称以驼峰格式命名,所以 index.vue 会报错。

image.png

二、解决方案

  • 一、按照规则,使用驼峰命名,例如 AppHeader.vue

  • 二、使用简单粗暴的方法,vue.config.js关闭eslint校验

image.png

以上方案治标不治本,只是编译时不报错,如果使用vscode+eslint 会在文件头标红提示,并且官方并不建议直接关闭校验,有时候还是需要团队有个共同规范,所以都不是很好的解决方案!

二、通过配置文件解决方案

在根目录下找到  .eslintrc.js 文件,同样如果没有则新建一个(注意文件前有个点),代码如下

module.exports = {
    root: true,
    env: {
        node: true
    },
    'extends': [
        'plugin:vue/essential',
        'eslint:recommended'
    ],
    parserOptions: {
        parser: '@babel/eslint-parser',
        "requireConfigFile": false  //解决文件名爆红问题
    },
    rules: {
        'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
        'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
        //在rules中添加自定义规则
        //关闭组件命名规则---没有指定具体文件
       // "vue/multi-word-component-names":"off",
    },
    overrides: [
        //这里是添加的代码
        {
            files: ['src/components/index.vue','src/components/**/index.vue'],   // 匹配components和views下二级目录中的index.vue
            rules: {
                'vue/multi-word-component-names':"off",
            } //给上面匹配的文件指定规则
        },
        {
            files: [
                '**/__tests__/*.{j,t}s?(x)',
                '**/tests/unit/**/*.spec.{j,t}s?(x)'
            ],
            env: {
                jest: true
            }
        }
    ]
}