uniapp 配置webpack-dev-server代理 解決開發環境跨域問題

項目根目錄下創建 vue.config.js

module.exports = {
  devServer: {
    proxy: {
      '/api': {//代理 /api/xxx/xxx 的請求
        target: '你要請求接口的地址',
        pathRewrite: {
          '^/api': ''
        }
      }
    },
  }
}

開發環境的 BASE_URL 如下
const BASE_URL = "/api";
request 的 url 爲 /xxx/yyy
會被代理到 target/xxx/yyy

參考:

https://www.webpackjs.com/configuration/dev-server/#devserver

https://cli.vuejs.org/zh/config/#devserver

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章