axios({
??? method:'post',
??? url: '/user/123',
??? data:{
??????? msg: '123'
? ? }
})
axios.get("url",{? ? params:{ },? headers:{ token:"jack"}? ? }).then(res=>{? }).catch(function (error) {? });
axios.post("url",{},{? headers:{? token:"tom"? }? }).then(res=>{? }).catch(err=>{ })
axios.delete('url?id='+id).then()
axios全局配置
axios.defaults.timeout = 1000;? //請求超時時間
axios.defaults.baseURL =? 'localhost:8080';? //默認(rèn)地址前戳?
axios.defaults.headers.post['Content-Type'] = 'application/json'? ?//POST參數(shù)格式
axios實例配置
let axios1 = axios.create();
//?創(chuàng)建axios實例?//?請求超時時間var?instance?=?axios.create({????timeout:?1000?*?12,????withCredentials:?true,????baseURL:?'/api',????})
axios1.default.timeout = 3000;
axios請求配置
axios1.get( url,{ timeout: 5000 } )
攔截器: 在請求或者響應(yīng)被處理前攔截它們----請求攔截器? <==>? 響應(yīng)攔截器
請求攔截器??
axios.interceptors.request.use( config => { /*發(fā)送前做些什么*/ config.hearders['token'] = ''? return config},err => { /*請求錯誤做些什么*/ return Promise.reject(err) } )
取消攔截器? axios.interceptors.request.eject(interceptors)
axios.all( [? ?//并發(fā)請求
? ? axios.get(url),
? ? axios.get(url)
] ).then(?
? ? axios.spread( (get1,get2)=>{? ?//對應(yīng)返回參數(shù)
? ? ?} )
)
file文件上傳
<input type="file" @change="getFile" />?
getFile(e){ e.target.files[0] }
var fd = new FormData();
fd.append('file',e.target.files[0])
this.$axios.post('upload',fd,{
cancelToken: source.token, // 攜帶取消標(biāo)識
? onUploadProgress: (progressEvent) => {
????? this.fileLoad = Math.ceil( (progressEvent.loaded / progressEvent.total)*100 ); //上傳進度
? }
})
var source = axios.CancelToken.source();
source.cancel( '請求被取消了' )