美文网首页
vue axios 安装 使用以及代理跨域

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

作者: 拾钱运 | 来源:发表于2019-02-28 10:17 被阅读2次
    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':'/'
            }
          }
        },
    

    即可

    相关文章

      网友评论

          本文标题:vue axios 安装 使用以及代理跨域

          本文链接:https://www.haomeiwen.com/subject/ouacuqtx.html