webpack中代理配置(proxyTable)

本文介绍如何在Vue项目中使用config文件下的index.js配置代理,实现localhost:8080到目标API的代理,以及如何利用Axios进行资源请求。详细解释了proxyTable的配置方法,包括target、changeOrigin等参数的使用,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在config文件下的index.js中配置代理地址

 

   参考:https://vuejs-templates.github.io/webpack/proxy.html
  举例:localhost:8080 代理到 http://www.123.com/api,如图
    如果用pathRewrite重写则代理到http://www.123.com/api
  将'/api'转为'/'
  
proxyTable: {
      '/api': {// '/api':匹配项
        target: 'http://www.123.com/',// 接口的域名
     // secure: false,// 如果是https接口,需要配置这个参数
        changeOrigin: true,// 如果接口跨域,需要进行这个参数配置
     // pathRewrite: {// 如果接口本身没有/api需要通过pathRewrite来重写了地址
     //   '^api': ''
        // }


      }
    }

 

用axios请求资源

参考:https://www.npmjs.com/package/axios
//引入axios
import axios from 'axios'
export function getProductTree() {
 // 用axios.get()请求资源
  return axios.get('/api/pageblock/getProductCategoryTree')
}

页面中请求资源

import { getProductTree } from 'api/product-tree.js'

export default {
  created() {
    this._getProductTree()
  },
  methods: {
    _getProductTree() {
      // 页面加载时请求资源
      getProductTree().then(data => {
        console.log(data)
      })
    }
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值