vue配置代理为什么没起作用

服务器端口为4000,postman测试没有问题:

img

vue.config.js如下:

module.exports = {
  devServer: {
    host: 'localhost',
    port: 8080,
    proxy: {
      '/api': {
        target: 'http://localhost:4000',// 要跨域的域名
        changeOrigin: true, // 是否开启跨域
      },
    }
  }
}

在App.vue中测试代理:

    mounted() {
            axios.get("http://localhost:8080/api/index_category").then(
            response=>{
                console.log(response.data)
            },error=>{
                console.log(error)
            }
            )
    },

控制台显示代理没有起作用:

img


奇怪的是我在一个新的vue项目中使用同样的方法测试没有问题:

img


难道说还要改啥其它配置嘛,有没有遇到过的赐教

你用法 错了吧
试试下面的,不行的话,去掉 /api前面的/


mounted() {
            axios.get("/api/index_category").then(
            response=>{
                console.log(response.data)
            },error=>{
                console.log(error)
            }
            )
    },

大佬带带我😭😭😭