设置代理,既可以统一域名,又简单省事,十分好用。以下是设置和使用方法。
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);
});
代理成功!!!