vue webpack腳手架的代理使用方法

設置代理,既可以統一域名,又簡單省事,十分好用。以下是設置和使用方法。

1、在config/index.js中,找到dev,再找到proxyTable屬性,然後配置

    proxyTable: {
      '/api':{    //設置代理的名字
        target: 'http://gengda.io',    //需要代理的地址
        changeOrigin:true,    //開啓跨域
        pathRewrite:{    
          '^/api':'/index.php/api'    
        }                    
      }
    }

配置後,‘/api’代表的地址是 http://gengda.io/index.php/api

使用方法如下:

            var server=new this.axios.create({
                transformRequest: [//對數據轉換成類似get傳參的模式
                    data => {
                        data=qs.stringify(data);
                        console.log(data);
                        return data;
                    }
                ]
            })
            server.post('/api/index/message',{
                    'id':'12346',
                    'user':'name'
            }).then(function(res){
                console.log(res);

            }).catch(function(error){
                console.log(error);
            });

代理成功!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章