Error in v-on handler (Promise/async): "[object Object]"

Error in v-on handler (Promise/async): "[object Object]"
请求函数中错误抛出异常return Promise.reject(res),在组件中使用如果不加try catch就会报错

request.interceptors.response.use(function (response) {
  // 获取到接口的返回值
  let res = response.data;

  // 只有是0的时候才是成功:
  if (res.status == 0) {
    return res.data;
  } else {
    // 弹错其他错误:
    return Promise.reject(res) // 需要用trycatch捕获异常
  }
}, (error) => {
  let res = error.response;
  return Promise.reject(error);
})

组件

try {
  const result = await reqLogin(username, password)
  console.log(result)
} catch (error) {
  alert(result.msg)
}

Error: Avoided redundant navigation to current location: "/index".

降级版本或在router.js页面写

const originalPush = VueRouter.prototype.push
   VueRouter.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}
posted @ 2020-09-13 23:14  yx1102  阅读(19290)  评论(1编辑  收藏  举报