Vite环境下Vue路由导入AuthComp组件失败问题求助
问题排查与解决
1. 修复路由数组的语法错误
你的router/index.js里routes数组开头多了一个逗号,这会导致语法解析异常,先删掉这个多余的逗号:
const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ { // 移除前面的逗号 path: '/auth', name: 'authentication', component: () => import('../components/authorisation/AuthComp') } ] })
2. 检查文件路径与命名
补充文件后缀
Vite默认不会自动补全.vue后缀(除非你在vite.config.js里配置了resolve.extensions包含.vue),尝试在导入路径后加上.vue:
component: () => import('../components/authorisation/AuthComp.vue')
核对拼写与大小写
- 确认
src/components/下存在authorisation文件夹,拼写完全一致(注意是英式拼写authorisation,别和美式authorization混淆) - 确认文件夹内确实有
AuthComp.vue文件,文件名的大小写要和导入语句里的完全匹配(Linux/macOS系统严格区分大小写,Windows不区分,但建议统一避免跨环境问题)
3. 确认项目文件结构
你的项目层级需要符合以下结构(确保路由文件和组件文件的层级对应):
src/ ├── router/ │ └── index.js └── components/ └── authorisation/ └── AuthComp.vue
如果以上都检查无误,重启Vite开发服务器试试,有时候缓存会导致路径解析异常。
内容的提问来源于stack exchange,提问作者Bash
相关产品推荐
相关产品推荐

