我对Vite+Vue3项目有个问题。
我使用Vite代理通过api获取数据,它在本地运行良好。
但是当部署到Vercel.com时会导致404错误。
Vite配置是这样的。
// vite.config.ts
server: {
port: 4000,
proxy: {
// 选项写法
'/api': {
target: 'http://xxx.xxx.xxx.xxx:9998',
changeOrigin: true,
rewrite: path => path.replace(/^\/api/, '')
}
},
hmr: {
overlay: false
},
host: '0.0.0.0'
},
我使用了这个模板:https://github.com/kailong321200875/vue-element-plus-admin
它被部署到http://his-lemon.vercel.app/
此错误与Vite 2和Vite 3中的错误相同。
我怎么才能解决这个问题?
发布于 2022-09-23 06:24:45
您需要在服务器端指定URL重写。对于Vercel,可以将vercel.json文件添加到项目的根目录中。
{
"rewrites": [
{ "source": "/api/:path(.*)", "destination": "http://xxx.xxx.xxx.xxx:9998/:path" },
{ "source": "/(.*)", "destination": "/index.html" }
]
}
如果部署到Netlify,则应该在公用文件夹th中提供一个_redirects文件,如下所示
/api* http://xxx.xxx.xxx.xxx:9998/:splat 200
/* /index.html 200
https://stackoverflow.com/questions/73267938
复制相似问题