• Vue框架总结(五、Vue配置代理)


    一、Vue脚手架配置代理

    1. 案例引入

    在这里插入图片描述


    在本机服务中,开放了80858086端口分别用于获取学生与汽车信息。

    我们在 Vue 中使用 axios 请求数据

    • 首先安装 axios
    npm install axios
    
    • 1
    • 引入 axios
    import axios from 'axios'
    
    • 1

    src/App.vue

    <template>
      <div id="app">
        <img alt="Vue logo" src="./assets/logo.png">
        <button @click="getStudent">获取学生信息button>
      div>
    template>
    
    <script>
    
    import axios from 'axios'
    
    export default {
      name: 'App',
    
      methods: {
    
        getStudent(){
          axios({
            method: "post",
            url: "http://localhost:8085/students"
          }).then(function (resp) {
            if(resp.data.code !== 1) {
              _this.$message.error(resp.data.msg || "网络出了点小差错~~~");
            }else{
              console.log("data = ", resp.data)
            }
          })
        }
      },
    }
    
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    在这里插入图片描述


    当直接请求时发生了跨域问题,在这里,主要是由于端口号不同。要解决的方法也有很多,在此处,我们可以使用Vue中的配置代理方式。

    2. 配置代理_方式1

    打开 vue.config.js 文件,配置代理。

    在这里插入图片描述

    const { defineConfig } = require('@vue/cli-service')
    module.exports = defineConfig({
    	transpileDependencies: true,
    	lintOnSave: false, // 关闭语法检查
    	
    	// 开启代理服务器
    	devServer:{
    		proxy: "http://localhost:8085"
    	}
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    再将src/App.vue中的请求路径端口改为vue服务启动端口(8080

    <script>
    import axios from 'axios'
    
    export default {
      name: 'App',
    
      methods: {
    
        getStudent(){
          axios({
            method: "post",
            // 请求端口改为 8080
            url: "http://localhost:8080/students"
          }).then(function (resp) {
            if(resp.data.code !== 1) {
              _this.$message.error(resp.data.msg || "网络出了点小差错~~~");
            }else{
              console.log("data = ", resp.data)
            }
          })
        }
      },
    }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述


    配置代理_方式1:

    • 优点:配置简单,请求资源时直接发给前端即可
    • 缺点:不能配置多个代理,不能灵活的控制请求是否走代理
    • 工作方式:若按照上述配置代理,当请求了前端不存在的资源时,那么该请求会转发给服务器 (优先匹配前端资源)

    3. 配置代理_方式2

    同样是在vue.config.js中做配置

    devServer: {
        proxy: {
          	'/api1': { // 匹配所有以 '/api1'开头的请求路径
            	target: 'http://localhost:8085',// 代理目标的基础路径
            	changeOrigin: true,
            	// 将 /api1 替换为 空字符串,否则发送至后端时的 url 仍带有 /api1
            	pathRewrite: {'^/api1': ''}
          	},
          	'/api2': { // 匹配所有以 '/api2'开头的请求路径
            	target: 'http://localhost:8086',// 代理目标的基础路径
            	changeOrigin: true,
            	// 将 /api2 替换为 空字符串,否则发送至后端时的 url 仍带有 /api2
            	pathRewrite: {'^/api2': ''}
          	}
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    src/App.vue

    需要在请求路径端口号添加vue.config.js配置的路径名(例如api1

    <template>
      <div id="app">
        <img alt="Vue logo" src="./assets/logo.png">
    
        <button @click="getStudent">获取学生信息button>
    
        <button @click="getCars">获取汽车信息button>
      div>
    template>
    
    <script>
    import axios from 'axios'
    
    export default {
      name: 'App',
    
      methods: {
    
        getStudent(){
          axios({
            method: "post",
            url: "http://localhost:8080/api1/students"
          }).then(function (resp) {
            if(resp.data.code !== 1) {
              _this.$message.error(resp.data.msg || "网络出了点小差错~~~");
            }else{
              console.log("students data = ", resp.data)
            }
          })
        },
    
        getCars(){
          axios({
            method: "post",
            url: "http://localhost:8080/api2/cars"
          }).then(function (resp) {
            if(resp.data.code !== 1) {
              _this.$message.error(resp.data.msg || "网络出了点小差错~~~");
            }else{
              console.log("cars data = ", resp.data)
            }
          })
        }
      }
    }
    
    
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    在这里插入图片描述


    配置代理_方式2:

    • 优点:可以配置多个代理,且可以灵活的控制请求是否走代理
    • 缺点:配置略微繁琐,请求资源时必须加前缀

  • 相关阅读:
    无处不在的边缘网络感知
    【学习笔记】快速幂
    DBA数据库运维-MySQL安装篇(glibc,源码)
    uni-app--》基于小程序开发的电商平台项目实战(一)
    新手看过来----代码自测通过但作业通不过
    STP生成树协议基本配置示例---STP逻辑树产生和修改
    Python Django路由urls.py详解
    vue2循环的列表商品选择后的商品禁用
    logistic回归列线图(nomogram)的多种绘制方法
    用QT做一个rtsp / rtmp实时流的播放器 ffmpeg
  • 原文地址:https://blog.csdn.net/qq_51938362/article/details/126255991