在vuejs中使用axios方法处理错误

问题描述 投票:0回答:1

如果得到这样的响应 - data:Array(0)length:0

如何处理错误以及如何在vuejs中找到没有匹配数据的消息。

vue.js
1个回答
0
投票

首先,如果使用async / await,您应该使用try / catch包装axios请求,或者只使用then..catch方法。

这是一个简单的例子

axios.get('http://api.com')
.then((response) => {
 if (response.data.length === 0) { // Lets check if response contains any items
   // Do Your 'no items found' logic here
   console.log('No items found')
   return
 } else { // We have items, handle them here!
   // We have some items, lets log them
   console.log(response.data)
 }
})
.catch((error) => {
  // Catch and handle any errors here
  console.log(error)
})
© www.soinside.com 2019 - 2024. All rights reserved.