注冊
如果在Vue中使用,npm安裝后,通過
import Vue from 'vue'
import axios from 'axios'
Vue.prototype.axios = axios
掛載到Vue實例上
使用(兩種方法):
this.axios.get(url).then(response => { // axios的get/post方法本身就是一個Promise對象
console.log(response)
}).catch((error) => {
console.log(error)
}
)
async function(){
let response = await this.axios.post(url,data); // response是一個response對象,已經被封裝好了
console.log(reponse);
console.log(response.data, response.status); // 注意status如果是404,則會在await時候報錯!!!根本得不到response
}
結果返回:
{
data:xxxx,
status:200,
headers:{xxx},
request:XMLHttpRequest {xxx},
statusTEXT:'ok',
__proto__: Object
}
由此可以根據返回對象判斷結果,但是這並不能表明:對於Promise對象,可以用await獲取內部的值