vue - axios网络模块封装
2020-03-18 本文已影响0人
廖小芝
axios 安装
npm install axios --save
axios 请求方式
1、axios(config) 默认get
import axios from 'axios'
axios({
url:''
,params:{
limit:'20',
page:1,
uid:0,
status:1
}
}).then(res =>{
console.log(res);
})
2、axios 的并发请求
axios.all([axios({
url:'',
method:'post',
data:{
id:1,
name:'xxx'
}
}), axios({
url:''
,params:{
limit:'20',
page:1,
uid:0,
status:1
}
})]).then(axios.spread( (res1 , res2) =>{
console.log(res1)
console.log(res2)
}))
3、axios 的全局配置
axios.defaults.baseURL = 'http://xxx.top'
axios.defaults.timeout = 5000
axios.all([axios({
url:'/gzhList'
}), axios({
url:'/countlist'
,params:{
limit:'20',
page:1,
uid:0,
status:1
}
})]).then(axios.spread( (res1 , res2) =>{
console.log(res1)
console.log(res2)
}))
4、axios 的实例
const instance1 = axios.create({
baseURL : 'http://xxx1.top/index.php',
timeout : 5000
})
instance1({
url:'/recharge/Index/gzhList'
}).then( res =>{
console.log(res)
})
const instance2 = axios.create({
baseURL : 'http://xx2/index.php',
timeout : 10000
})
instance2({
url:'/gzh/gzhdata/countlist',
params:{
limit:'20',
page:1,
uid:0,
status:1
}
}).then( res =>{
console.log(res)
})