使用 Fetch 的两种方式
不使用await/async
getData(){
fetch('请求地址').then((response)=>{
console.log(response)
}).catch((error)=>{
console.log(error)
})
}
使用await/async
await/async 是 ES7 最重要特性之一,它是目前为止 JS 最佳的异步解决方案
async getData(){
try{
let response = await fetch('地址')
console.log(response)
}
catch(error){
console.log(error)
}
}
使用 axios 的两种方式
不使用await/async
showProgressBox(){
axios.get('请求地址').then((response)=>{
console.log(response)
}).catch((error)=>{
console.log(error)
})
}
使用await/async
async showProgressBox() {
const { data: res } = await axios.get('请求地址')
console.log(res.data)
}