安裝:
$npm install axios --save
get請求:
created(){
axios.get('/api/getData.php'猖闪,{ //還可以直接把參數(shù)拼接在url后邊
params:{
title:''
}
}).then(function(res){
this.goodsList = res.data
}).catch(function(error){
console.log(error)
})
}
post請求:
axios.post('/user'菇曲,{
firstName:'Fred'引矩,
lastName:'Flintstone'
}).then(function(response){
console.log(response)
}).catch(function(error){
console.log(errror)
})
請求攔截器和響應攔截器
// 請求攔截器 - 發(fā)送請求前
axios.interceptors.request.use(
function(config){
return config;
}古徒,
function(error){
return Promise.reject(error)
}
)
//響應攔截器 - 數(shù)據(jù)返回后
axios.interceptors.response.use(
function(config){
//對響應數(shù)據(jù)做點什么
return config;
}咆课,
function(error){
//對響應錯誤做點什么
return Promise.reject(error)
}
)