我想在开发模式下(npm run dev)将HTTP请求的基本URL从Vite应用程序的主机地址(http://localhost:5173)更改为我的ASP.NET API的主机地址(http://localhost:5815).

enter image description here

但在将代理应用于重定向请求后,我在运行我的Vite-Vue应用程序时出现了这样的错误:

enter image description here

我的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/, '')
      }
    }
  }
})

结果是:

enter image description here

我应该做什么才能将我的所有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',
  },
});

推荐答案

在任何情况下,您都需要一个不同于根目录的路径(因为从根目录开始加载网站的assets资源 ,如css、js和其他文件).

export default defineConfig({
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:5815',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''), // It removes the /api from the request address, so it will truly be used only for differentiation
      },
    },
  },
})

所有与/api相关的地址都将重定向到localhost:5815.在示例中,您可以看到访问localhost:5815/some-endpoint:

// Before
axios.get('/some-endpoint') // With what you are currently trying, it will never work from the root
// Call http://localhost:5173/some-endpoint (Vite Host)

// After
axios.get('/api/some-endpoint') // With the /api proxy route defined now, it works
// Call http://localhost:5815/some-endpoint (Your Custom API)

如何在现场生产版本中修改此目标代理地址?只需判断VITE当前是否在dev或prod模式下运行.因此,可以动态地设置地址.

const API_URI = process.env.NODE_ENV === 'production'
  ? 'http://example.com'    // in prod
  : 'http://localhost:5815' // in dev

export default defineConfig({
  server: {
    proxy: {
      '/api': {
        target: API_URI,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
    },
  },
})

Vue.js相关问答推荐

Vue CDN的html工作,但v—如果不工作

无法从CDN使用Vue和PrimeVue呈现DataTable

Vuetify右侧并排switch

在 Vuejs 中更新 Chartjs 图表数据集

nuxt.js - 预加载 .woff 字体加载为@font-face

Vuex 和 Event Bus 有什么区别?

如何在 Vue 中编译从外部 api 加载的模板

如何为 Vue 应用提供 robots.txt

使用 Vue.js (cdn) 在特定元素上切换显示/隐藏

使用 VueJS 在 v-for 中转换

在 Vue.js 中,为什么我们必须在导入组件后导出它们?

如何在 Vue 中动态创建组件上获取更新的 $refs?

如何将数据传递给 Vue.js 中的路由视图

如何在 Vue.js 应用程序中正确下载 Excel 文件?

Vue如何将动态ID与v-for循环+字符串中的字段连接起来?

vuejs 配置:使用全局变量?

如何在 vue.js 中进行嵌入?

在 Vue 应用程序中创建一个类的单个实例

Axios 捕获错误请求失败,状态码 404

表格行上的 Vuejs 转换