使用axios实现后台的数据交互
1.首先在项目文件目录下安装 axios npm install –save axioa
2.在项目的main.js中引入axios import axios from ‘axios’;
3.改变原型使用axios1
2axios.default.withCredentials = true;
Vue.prototype.$axios = axios;
4.在config文件的index.js配置axios1
2
3
4
5
6
7
8
9proxyTable: {
'/api': {
target: 'http://dev.njga.kefu.com:8090/', //设置你调用的接口域名和端口号,别忘了加http
changeOrigin: true,
pathRewrite: {
'^/api': '/' //用 '/app'代替target里面的地址,后面再调用接口的时候直接用api代替,比如我要调用'http://40.00.100.100:3002/user/add',直接写‘/api/user/add’即可
}
}
}
5.实现请求
初始化请求1
2
3
4
5
6
7
8
9
10
11
12const beforeMounte = function(){
console.log('beforeMounte');
axios.post('/api/clientservicecenter/clientservicecenterapi/dialog_list',{
page: '0',
page_size: '10',
cs_id: '100000005'
}).then(function(response){
console.log(response);
}).catch(function(error){
console.log(error);
});
}
然后执行1
2
3new Vue({
beforeMount: beforeMounte, //在挂载前执行
})