如果是用 Vite 创建的Vue项目,在使用 import HelloWorld from './components/HelloWorld' 引入组件时是会有错误提示 Failed to resolve import "./components/HelloWorld" from "xxx". Does the file exist? 的,因为 Vite 并不会自动添加 .vue 后缀。 因为默认的后缀只有 ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'],具体可以查看 👉 #resolve.extensions - 共享选项 | Vite 官方中文文档
OP 你当前项目使用的是 VueCLI 还是 Vite?
我这边使用 VueCLI (
5.x
)创建了一个空白项目,在使用import HelloWorld from './components/HelloWorld'
引入组件时,会自动引入./components/HelloWorld/index.vue
或者./components/HelloWorld.vue
文件。所以如果是使用的 VueCLI 创建的项目,版本号是多少,是否有在
vue.config.js
当中对resolve.extensions
和resolve.mainFields
做了单独的配置?还是说因为你开启了 vue/multi-word-component-names 这个Lint规则,所以因为检查没有通过导致的编译失败?
如果是用 Vite 创建的Vue项目,在使用
import HelloWorld from './components/HelloWorld'
引入组件时是会有错误提示Failed to resolve import "./components/HelloWorld" from "xxx". Does the file exist?
的,因为Vite
并不会自动添加.vue
后缀。因为默认的后缀只有
['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json']
,具体可以查看 👉 #resolve.extensions - 共享选项 | Vite 官方中文文档如果你想要自动引入可以手动修改
resolve.extensions
这个配置项添加.vue
后缀。