vite.config.ts 配置别名报错
环境配置 vue3 + ts
配置如下:(vite.config.ts)
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
const path = require('path')
function resolve(dir: string) {
const _path: string = path.resolve(__dirname, dir);
return _path;
}
// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: {
'/@/': resolve('src'),
// '/@component/': resolve('src/components'),
'/@components/': path.resolve(__dirname, './src/components')
}
},
plugins: [vue()]
})
报错如下:
html:C:/Users/**/Desktop/vite-project/src/App.vue:4:23: error: Could not resolve "/@components/HelloWorld.vue" (the plugin "vite:dep-scan" didn't set a resolve directory)
4 │ import HelloWorld from '/@components/HelloWorld.vue'
error when starting dev server:
Error: Build failed with 1 error:
html:C:/Users/**/Desktop/vite-project/src/App.vue:4:23: error: Could not resolve "/@components/HelloWorld.vue" (the plugin "vite:dep-scan" didn't set a resolve directory)
at failureErrorWithLog (C:\Users\fw0009626\Desktop\vite-project\node_modules\esbuild\lib\main.js:1443:15)
at C:\Users\fw0009626\Desktop\vite-project\node_modules\esbuild\lib\main.js:1125:28
at runOnEndCallbacks (C:\Users\fw0009626\Desktop\vite-project\node_modules\esbuild\lib\main.js:915:63)
at buildResponseToResult (C:\Users\fw0009626\Desktop\vite-project\node_modules\esbuild\lib\main.js:1123:7)
at C:\Users\fw0009626\Desktop\vite-project\node_modules\esbuild\lib\main.js:1230:14
at C:\Users\fw0009626\Desktop\vite-project\node_modules\esbuild\lib\main.js:606:9
at handleIncomingPacket (C:\Users\fw0009626\Desktop\vite-project\node_modules\esbuild\lib\main.js:703:9)
at Socket.readFromStdout (C:\Users\fw0009626\Desktop\vite-project\node_modules\esbuild\lib\main.js:573:7)
at Socket.emit (events.js:315:20)
at addChunk (internal/streams/readable.js:309:12) ╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
vite 版本 2.3.3 目前已经解决
vite.config.ts 配置如下:
页面引用如下:
vite版本2.6.4,ts版本4.4.3,按照下面这种方式配置也可以
页面引用如下