gpt4 book ai didi

javascript - Vite: resolve.alias - 如何解析路径?

转载 作者:行者123 更新时间:2023-12-04 14:55:05 26 4
gpt4 key购买 nike

有什么可以resolve.alias做?它不解决以下路径:

// vite.config.js
import { defineConfig } from 'vite'
import path from 'path'

export default defineConfig({
resolve: {
alias: {
'@': path.resolve(__dirname, '/src'),
},
}
})
在我的 HTML 中:
<img src="@/assets/images/sample-1.jpg">
浏览器控制台中的错误:
GET http://localhost:3000/@/assets/images/sample-1.jpg 
Failed to load resource: the server responded with a status of 404 (Not Found)
client:180 [vite] connecting...
client:202 [vite] connected.
任何想法如何正确地做到这一点?

最佳答案

npm install @rollup/plugin-alias --save-dev
或者
yarn 添加 -D @rollup/plugin-alias

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import alias from '@rollup/plugin-alias'
import { resolve } from 'path'

const projectRootDir = resolve(__dirname);

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue(),
alias({
entries: [
{
find: '@',
replacement: resolve(projectRootDir, 'src')
}
]
})
],
server: {
host: '0.0.0.0',
port: 10086,
open: false,
cors: true,
},
build: {
outDir: 'dist',
}
})

或者

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import alias from '@rollup/plugin-alias'
import { resolve } from 'path'

const projectRootDir = resolve(__dirname);

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
alias(),
vue()
],
resolve: {
alias: {
"@": resolve(projectRootDir, "src"),
},
},
server: {
host: '0.0.0.0',
port: 10086,
open: false,
cors: true,
},
build: {
outDir: 'dist',
}
})


关于javascript - Vite: resolve.alias - 如何解析路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68217795/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com