发布网友 发布时间:2022-04-23 15:17
共1个回答
热心网友 时间:2022-04-20 02:39
全局使用Axios
首先,在自己建的公用方法的文件中new一个新的HttpUtil.js文件。以下为HttpUtil.js的内容:
var axios = require('axios')// 配置项目根如路径var root = 'http://localhost:8090/manage'// axios请求function httpApi (method, url, params) { return new Promise((resolve, reject) => { axios({ method: method, url: url, data: method === 'POST' || method === 'PUT' ? params : null, params: method === 'GET' || method === 'DELETE' ? params : null, baseURL: root, withCredentials: false }).then(
(response) => {
resolve(response)
}
).catch(
(error) => {
reject(error)
}
)
})
}// 返回在vue模板中的调用接口export default { get: function (url, params) { return httpApi('GET', url, params)
}, post: function (url, params) { return httpApi('POST', url, params)
}, put: function (url, params) { return httpApi('PUT', url, params)
}, delete: function (url, params) { return httpApi('DELETE', url, params)
}
}