zoukankan      html  css  js  c++  java
  • vue-cli 跨域配置 proxyTable

    以下是vue-cli 2 中 config/index.js文件中的代码

    module.exports = {
      dev: {
    
        // Paths
        assetsSubDirectory: 'static',
        assetsPublicPath: '/',
        proxyTable: {
          '/ggg':{ //以 /ggg 开头的URL就会走代理
            target: 'http://127.0.0.1:3000', //一定注意这里不管是IP,还是localhost 都要加上 http://
            changeOrigin: true, //开启代理,跨域
            pathRewrite: {
              '^/ggg': ''  // 什么?,后端请求的URL中没有 /ggg, 那我们把他删了,重写为空字符串
            }
          }
        },
    pathRewrite 这个词刚学的时候 不太好理解
    比如 我本地起的项目是 http://localhost:8080 需要请求 http://localhost:3000的接口
    
    举例
    1.如果不加这个字段 当访问 /ggg/api/list的时候 就会请求 http://localhost:3000/ggg/api/list 的内容
    
    2.如果加了字段 pathRewrite:{ '^/ggg':''} 就会访问 http://localhost:3000/api/list 的内容 (/ggg 变成了空)
    3.如果加了字段 pathRewrite:{ '^/ggg':'/get'} 就会访问 http://localhost:3000/get/api/list 的内容
    
    
    我本地写的请求

    浏览器发送的请求如下图,响应数据就不写了

    如果需要mock数据 点击这个 git-mock (koa2写的)可供参考使用

    
    




  • 相关阅读:
    C#将一个字符串数组的元素的顺序进行反转
    C#找出100内所有的素数/质数
    C#流程控制for循环语句,水仙花数。
    C# 常用的操作文件夹的方法
    Element UI
    JS
    JS
    PHP基础算法
    js实现csv下载
    el-dialog“闪动”解决办法
  • 原文地址:https://www.cnblogs.com/hill-foryou/p/11218097.html
Copyright © 2011-2022 走看看