Vue 配置代理 完整 axios

一、配置文件

vue.config.js

复制代码
  devServer: {
    proxy: {
      '/api': {
        // 获取数据的接口
        target: 'http://127.0.0.1:8000',
        // 特别重要 
        pathRewrite:{'^/api':''},
        ws: true,
        changeOrigin: true
      },
    }
  }
复制代码

二、请求数据

1、下载axios

npm install axios

2、引入

import axios from 'axios';

3、异步(注意端口后面直接加代理)

复制代码
methods: {
    getCars(){
        axios.get('http://localhost:8080/api/cars/').then(
            response=>{
                console.log(response.data)
            },
            error=>{
                console.log(error.message)
            }
        )
    }
},
复制代码

三、多个代理,注意pahtRewrite

复制代码
module.exports = {
  devServer: {
    proxy: {
      '/api': {
        target: '<url>',
        ws: true,
        changeOrigin: true
      },
      '/foo': {
        target: '<other_url>'
      }
    }
  }
}
复制代码

 

posted @   市丸银  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
历史上的今天:
2020-01-06 java 语句流程
点击右上角即可分享
微信分享提示