Vue全局使用axios
main.js
- 在main.js中引入以后就可以在所有组件中通过 this.$axios({...}) 使用
import axios from 'axios'
Vue.prototype.$axios = axios
Vue.prototype.$axios = Axios.create({
baseURL: 'http://localhost:3000'
})
其他组件中使用
this.$axios({
method: 'post',
url: path,
data: data
}).then(response => {
console.log(response.data)
console.log(response.status)
console.log(response.statusText)
console.log(response.headers)
console.log(response.config)
})
Axios 多请求处理
// 请求结果按请求顺序返回一个数组
this.$axios.all([
this.$axios.get(`http://localhost:3000`),
this.$axios.get(`http://localhost:3000`),
this.$axios.get(`http://localhost:3000`),
this.$axios.get(`http://localhost:3000`)
]).then(response => {
response.forEach((value, key) => {
console.log(value)
})
}
// 返回结果可以使用axios.spread()解析处理
this.$axios.all([
this.$axios.get(`http://localhost:3000`),
this.$axios.get(`http://localhost:3000`),
this.$axios.get(`http://localhost:3000`),
this.$axios.get(`http://localhost:3000`)
]).then(this.$axios.spread((data1, data2, data3, data4) => {
})
具体axios使用可以参考
http://www.jianshu.com/p/6c4bfe3dd0d8
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。