我想在开发模式下(npm run dev)将HTTP请求的基本URL从Vite应用程序的主机地址(http://localhost:5173)更改为我的ASP.NET API的主机地址(http://localhost:5815).
但在将代理应用于重定向请求后,我在运行我的Vite-Vue应用程序时出现了这样的错误:
我的vite.config.js文件:
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue(),
],
build: {
cssCodeSplit: false,
},
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
},
server: {
proxy: {
'/': {
target: 'http://localhost:5815',
changeOrigin: true,
rewrite: (path) => path.replace(/^\//, '')
}
}
}
})
我也try 了这样的vite.config文件
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue(),
],
build: {
cssCodeSplit: false,
},
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
},
server: {
proxy: {
'/api': {
target: 'http://localhost:5815',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
}
}
}
})
结果是:
我应该做什么才能将我的所有HTTP请求从 在开发模式下运行期间,本地主机:5173到本地主机:5815(NPM运行开发)?
我只提到了仅使用Vue的类似项目,该项目使用以下vue.config文件:
const { defineConfig } = require('@vue/cli-service');
module.exports = defineConfig({
transpileDependencies: true,
lintOnSave: false,
devServer: {
proxy: 'http://localhost:5815',
},
});