vue3 + vite 跨域 Cors


vite.config.ts

export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  server: {
    port: 3000,
    open: false, //自動打開 
    base: "./ ", //生產環境路徑
    proxy: { // 本地開發環境通過代理實現跨域,生產環境使用 nginx 轉發
      // 正則表達式寫法
      '^/api': {
        target: 'http://xxx.xxx.xxx.xxx:5021/api', // 后端服務實際地址
        changeOrigin: true, //開啟代理
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  },
});

使用

axios
   .get("api/Users")
   .then((response: { data: any }) => 
    {
       console.log(response.data)
    });


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM