为什么我的 vue.config.js 代理不起作用?

Mar*_*cel 9 config vue.js vue-cli-3

我有一个 vue cli 3.5.0 项目,正在尝试添加代理,但我无法让它工作。服务器运行在 :5000 上,客户端运行在 :8080 上。下面你可以看到我的代理。但它继续使用 :8080 而不是 :5000

vue.config.js

module.exports = {
    devServer: {
      proxy: {
        '/api': {
          target: 'http://localhost:5000',
          ws: true,
          changeOrigin: true
        }
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

这是我如何调用请求的示例

const url = '/api/races/'
const config = {
    headers: {'Authorization': "Bearer " + localStorage.token}
}

class RaceService {
    // Get Races
    static getRaces() {
        return new Promise(async (resolve, reject) => {
            try {
                const res = await axios.get(url)
                const races = res.data
                resolve(races)
            } catch (err) {
                reject(err)
            }
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我吗?api 请求需要使用 :5000 而不是 :8080

小智 3

您必须使用“^/api”而不是“/api”