首页 新闻 赞助 找找看

vue如何全局捕获try catch的异常

0
悬赏园豆:10 [待解决问题]

vue 通过axios调用后台接口,返回数据,在then方法里面接收到数据做一些业务逻辑,结果这个业务逻辑代码写错了,比如定义了const a=1; a=4;这样的错误 catch可以捕获到,但是如果每个请求都写catch,太麻烦了,如果使用全局捕获,测试发现 Vue.config.errorHandler 无法捕获这样的异常

代码小六的主页 代码小六 | 初学一级 | 园豆:12
提问于:2020-03-29 02:00
< >
分享
所有回答(1)
1

代码写错了,修改了就好了,我猜你应该是想要统一处理请求错误吧,用 axios 拦截器

axios.interceptors.response.use(function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    return response;
  }, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  })
by.Genesis | 园豆:2719 (老鸟四级) | 2020-03-29 21:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册