1添加一个新的 http.js文件 封装axios
//引入Axios import axios from 'axios'
//视你自己的接口地址而定 var root2 = 'http://121.4.63.196:8520/api'
//参数过滤(去空白) function filterNull(o) { for (var key in o) { if (o[key] === null) { delete o[key] } //字符串,对象,数组 if (typeof (o[key]) === 'string') { o[key] = o[key].trim() } else if (typeof (o[key] === 'object')) { alert(o[key]); o[key] = filterNull(o[key]) } else if (typeof (o[key]) === 'array') { o[key] = filterNull(o[key]) } } return o }
//接口处理函数 function apiAxios(method, url, params, success) { //整理参数 if (params) { params = filterNull(params) console.log(params); } axios({ method: method, url: url, data: method === 'post' || method === 'put' ? params : null, params: method === 'get' || method === 'delete' ? params : null, headers: { //'Content-Type':'application/json' }, baseURL: root2, withCredentials: false }).then(function (res) { if (res.State && res.Code) {
} }).catch(function (error) { let res = error.response if (error) { console.log('API错误:' + res.State); } }) }
//返回在vue模板中调用的接口 export default ({ get: function (url, params, success, failure) { return apiAxios('get', url, params, success, failure); }, post: function (url, params, success, failure) { return apiAxios('post', url, params, success, failure); }, put: function (url, params, success, failure) { return apiAxios('put', url, params, success, failure); }, delete: function (url, params, success, failure) { return apiAxios('delete', url, params, success, failure); } })
//引入接口文件 import api from './api/http.js' var app=createApp(App) app.use(routers)
//绑定给全局使用 $api可以随便定义 例如 $http $a $b 随喜好 app.config.globalProperties.$api=api
this.$api.post('/AutoCode/SendEmail163', { 'email': this.email },r=>{ this.email2=r.message });