0
点赞
收藏
分享

微信扫一扫

vue axios 安装 使用以及代理跨域

科牛 2021-09-24 阅读 66
随笔
npm install axios

在main.js中引入axios

import axios from 'axios'
Vue.prototype.$http = axios

这样在其他的vue组件中就可以this.$http 使用了 这个时候的/api是代理的名称
例如

 this.$http.post('/api/scrawl/save',{
          "aid":"1",
          "listname":that.list,
          "img":base64Data
        }
        ).then(function(res){
          console.log(res)
        }).catch(function(error){
          console.log(error)
        })

代理跨域 打开config 中index.js 找到proxyTable {}修改如下

  proxyTable: {
      '/api':{
        target:'http://www.xxx.com',
        changeOrigin:true,
        pathRewrite:{
          '^/api':'/'
        }
      }
    },

即可

举报

相关推荐

0 条评论