第一步: 安装 vue-resource
npm install vue-resource --save
第二步:在main.js中引入
import VueResource from 'vue-resource'
Vue.use(VueResource)
第三步:设置后台 (找到config文件夹下的index.js修改如下)
proxyTable: {
'/api':{
target:'后台地址',
changeOrigin:true,//允许跨域
pathRewrite:{
'/api':'/'
}
}
}
第四步:发送请求(这里用post请求)
this.$http({
method: 'POST',
url: 'api/login',
params: {
参数
xxx: xxx
}
}).then(function (res) {
成功!
}, function (res) {
失败!
})