vue-cli3跨域配置

vue-cli3跨域配置

版本是vue-cli3, 学习途中遇到跨域问题:

在这里插入图片描述

方法

添加vue.config.js

代码如下

module.exports = {
  baseUrl: './',    //我把这行删除了
  productionSourceMap: false,
  devServer: {
    proxy: {
      '/appz': {
        target: 'http://t.weather.sojson.com/api/weather/city/',
        changeOrigin: true,
        pathRewrite: {
          '^/appz': ''
        }
      },
      '/appy': {
        target: 'http://cdn.sojson.com/_city.json',
        changeOrigin: true,
        pathRewrite: {
          '^/appy': ''
        }
      }
    }
  }

}

/appz 将替换成接口的地址,
这里是写了两个接口的地址,一个就只要写一个,多个就复制。

这段参考源为:http://blog.51weblove.com/blog/180.html

接口调用案例

我这里用了个天气接口,地址在这
https://www.sojson.com/api/weather.html

export default {
  name: 'HelloWorld',
  data () {
    return {
      inputcity: ''
    }
  },
  methods: {
    searchW () {
      console.log('天气查询')
      this.$axios.get('/appz' + this.inputcity)
        .then(function (re) {
          console.log(re)
        })
        .catch(function (err) {
          console.log(err)
        })
    }
  }
}

nginx反向代理

有群友说这个方法只能本地用,打包后就行不通了。说用nginx反向代理。

等我打包时再更新这个问题。。。。
如下是nginx方法

https://www.cnblogs.com/xiangsj/p/8905648.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值