vue3 vite代理配置,解决跨域问题

vite.config.ts 根目录文件配置
export default defineConfig({
  plugins: [vue()],
  server: {
    port: 3000,
    open: true, //自动打开 
    base: "./ ", //生产环境路径
    proxy: { // 本地开发环境通过代理实现跨域,生产环境使用 nginx 转发
      // 正则表达式写法
      '^/api': {
        target: 'http://localhost:5067/api', // 后端服务实际地址
        changeOrigin: true, //开启代理
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  }
})

  

页面 axios  baseURL的配置,匹配代替地址
    axios.defaults.baseURL ='/api'
    let req = axios({ method: "post", url: "/Student/GetStudent" })
    req.then(res => {
        alert(JSON.stringify(res))
    })

  

posted @ 2022-11-07 16:17  杀马特、阿希  阅读(8428)  评论(0)    收藏  举报