$routeChangeError不在q.reject中调用

时间:2022-01-29 03:44:48


I have an Angular JS app with a Sails JS backend, and inside the routes (in app.js) I've got:

我有一个带帆JS后端的角JS应用程序,在路由中(在app. JS中)我有:

.state('app.detail', {
  url: "/detail",
  views: {
    'menuContent' :{
      templateUrl: "templates/detail.html",
      controller: 'UserUpdateCtrl',
      resolve: {
        auth: ["$q", "userData", function($q, userData) {
          var userInfo = userData.getUserInfo();
          if (userInfo) {
            return $q.when(userInfo);
          } else {
            return $q.reject({ authenticated: false });
          }
        }]
      },
    }
  }
})

(this is following this guide)

(以下是本指南)

Now on the same file, I have the $routeChangeError:

在同一个文件中,我有$routeChangeError:

.run(function($rootScope) {
  $rootScope.$on("$routeChangeError", function(event, current, previous, eventObj) {
    if (eventObj.authenticated === false) {
      $location.path("/login");
    }
  });

When debugging on chrome, I see that the function is defined, but not called.
What am I missing here?

在chrome上调试时,我看到函数已经定义了,但是没有调用。我错过了什么?

1 个解决方案

#1


11  

Okay so under the assumption you are using the Angular UI router, I believe you are just translating the error handler incorrectly from the official one.

假设你使用的是角UI路由器,我认为你只是把错误处理器从官方的错误处理器中错误地翻译出来。

From the docs for $state:

来自文件$state:

Fired when an error occurs during transition. It's important to note that if you have any errors in your resolve functions (javascript errors, non-existent services, etc) they will not throw traditionally. You must listen for this $stateChangeError event to catch ALL errors.

在转换过程中发生错误时触发。需要注意的是,如果您的解析函数中有任何错误(javascript错误、不存在的服务等等),它们通常不会抛出。您必须侦听$stateChangeError事件以捕获所有错误。

And the parameters for this handler are different, try something like this:

这个处理器的参数是不同的,试试这个

$rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error) {
  if (error && !error.authenticated) {
    $location.path("/login");
  }
});

Here's a description of the parameters with the important one bolded:

以下是对参数的描述,其中重要的一个用粗体表示:

  • event – {Object} – Event object.
  • 事件-{对象}-事件对象。
  • toState – {State} – The state being transitioned to.
  • toState - {State} -被转换为的状态。
  • toParams – {Object} – The params supplied to the toState.
  • toParams - {Object} -提供给toState的参数。
  • fromState – {State} – The current state, pre-transition.
  • fromState - {State} -当前状态,预转换。
  • fromParams – {Object} – The params supplied to the fromState.
  • fromParams - {Object} -提供给fromState的参数。
  • error – {Error} – The resolve error object.
  • 错误-{错误}-解决错误对象。

#1


11  

Okay so under the assumption you are using the Angular UI router, I believe you are just translating the error handler incorrectly from the official one.

假设你使用的是角UI路由器,我认为你只是把错误处理器从官方的错误处理器中错误地翻译出来。

From the docs for $state:

来自文件$state:

Fired when an error occurs during transition. It's important to note that if you have any errors in your resolve functions (javascript errors, non-existent services, etc) they will not throw traditionally. You must listen for this $stateChangeError event to catch ALL errors.

在转换过程中发生错误时触发。需要注意的是,如果您的解析函数中有任何错误(javascript错误、不存在的服务等等),它们通常不会抛出。您必须侦听$stateChangeError事件以捕获所有错误。

And the parameters for this handler are different, try something like this:

这个处理器的参数是不同的,试试这个

$rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error) {
  if (error && !error.authenticated) {
    $location.path("/login");
  }
});

Here's a description of the parameters with the important one bolded:

以下是对参数的描述,其中重要的一个用粗体表示:

  • event – {Object} – Event object.
  • 事件-{对象}-事件对象。
  • toState – {State} – The state being transitioned to.
  • toState - {State} -被转换为的状态。
  • toParams – {Object} – The params supplied to the toState.
  • toParams - {Object} -提供给toState的参数。
  • fromState – {State} – The current state, pre-transition.
  • fromState - {State} -当前状态,预转换。
  • fromParams – {Object} – The params supplied to the fromState.
  • fromParams - {Object} -提供给fromState的参数。
  • error – {Error} – The resolve error object.
  • 错误-{错误}-解决错误对象。