Vue项目编译报错:组件名"Posts"需遵循多词命名规则
Vue项目编译失败:ESLint组件名多词规则报错
问题代码(Vue Router配置)
import Vue from 'vue' import VueRouter from 'vue-router' import Posts from './views/Posts' //Next you need to call Vue.use(Router) to make sure that Router is added as a middleware to our Vue project. Vue.use(VueRouter) export default new VueRouter({ //The default mode for Vue Router is hash mode. //It uses a URL hash to simulate a full URL so that the page won’t be reloaded when the URL changes. mode: 'history', base: process.env.BASE_URL, routes: [ { path: '/', name: 'posts', component: Posts, }, ] })
编译错误信息
ERROR Failed to compile with 1 error 3:52:40 AM
[eslint]
C:\Users\Dell\Downloads\travel-test-main\django_vue\vue2\hello\src\views\Posts.vue
1:1 error Component name "Posts" should always be multi-word vue/multi-word-component-names
解决方法
方法1:重命名组件(推荐)
遵循Vue组件命名规范,将单词组件名改为多词格式:
- 将
Posts.vue文件重命名为PostList.vue(或PostCollection.vue等合理多词名称) - 在Router配置中同步修改导入和引用:
import PostList from './views/PostList' // ... routes: [ { path: '/', name: 'postList', component: PostList, }, ]
方法2:全局禁用ESLint该规则
若确实需要使用单词组件名,可在项目根目录的.eslintrc.js(或.eslintrc.json)中修改规则:
module.exports = { // ... 其他配置项 rules: { // ... 其他规则 'vue/multi-word-component-names': 'off' } }
方法3:给单个组件添加规则例外
仅让Posts组件跳过该规则检查,在.eslintrc.js中配置:
module.exports = { // ... 其他配置项 rules: { 'vue/multi-word-component-names': ['error', { ignores: ['Posts'] // 指定忽略的组件名 }] } }
内容的提问来源于stack exchange,提问作者umm-e- habiba
相关产品推荐
相关产品推荐

