过渡拒绝角度UI路由器

时间:2017-09-29 09:16:59

标签: angularjs angular-ui-router

我有一个HTTPInterceptor,每当捕获401时都应该进入登录状态。我的问题是我有3个异步调用API几乎同时启动。当所有这些都返回401时,我收到错误消息:

  

过渡拒绝($ id:0类型:6,消息:转换错误,详细信息:"未定义")

这是我的拦截器代码:

switch (response.status) {
  case 401:
    let url = $location.absUrl();
    // This condition supposed to prevent
    // going to signin state again after a response of 401.
    // Not really sure if ui-router will navigate still on the same
    // state. So maybe this condition is not needed?
    if (url.indexOf('sign-in') !== -1) {
      deferred.reject(response.data.error);
    } else {
      if ($transitions._transitionCount === 1) {
        $state.go('signin');
      }
    }
    break;

我到目前为止尝试的是检查transitionCount但仍然存在错误。

谢谢!

1 个答案:

答案 0 :(得分:5)

  

在UI-Router遗留问题中,用户经常抱怨做出结果   “燕子错误”。错误输出为$ stateChangeError事件,   但是应用程序有时候并没有听取此事。

     

在UI-Router 1.0中,我们添加了一个默认的错误处理程序。打电话的时候   $ state.go()失败,我们将错误传递给   StateService.defaultErrorHandler。

您可以使用

处理此类错误
$state.defaultErrorHandler(function(error) {
// This is a naive example of how to silence the default error handler.
console.log(error);
});

Details

相关问题