vue3+vite 解决本地调用时跨域请求

发布时间 2023-04-04 10:17:59作者: 映安

1、config配置文件中,axios默认请求地址;改为"",否则还是请求环境变量中的地址;

 

 2、vite.config.ts文件中做如下配置:

 

 

  server: {
    open: true, //启动项目自动弹出浏览器
    port: 8081, //启动端口
    cors: true,
    proxy: {
      "/api": {
      target: "http://192.168.0.128:8081",  // 要访问的地址
      changeOrigin: true,
      rewrite:path => path.replace(/^\/api/,'')
      },
    }
  }