gpt4 book ai didi

javascript - $routeChangeError 未在 $q.reject 上调用

转载 作者:可可西里 更新时间:2023-11-01 02:22:50 26 4
gpt4 key购买 nike


我有一个带有 Sails JS 后端的 Angular 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 guide )

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

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

在 chrome 上调试时,我看到函数已定义,但未被调用。
我在这里缺少什么?

最佳答案

好吧,假设您使用的是 Angular UI 路由器,我相信您只是错误地翻译了官方的错误处理程序。

来自docs for $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.

并且这个处理程序的参数不同,尝试这样的事情:

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

下面是参数的描述,重要的加粗:

  • 事件 – {Object} – 事件对象。
  • toState – {State} – 转换到的状态。
  • toParams – {Object} – 提供给 toState 的参数。
  • fromState – {State} – 当前状态,转换前。
  • fromParams – {Object} – 提供给 fromState 的参数。
  • error – {Error} – 解析错误对象。

关于javascript - $routeChangeError 未在 $q.reject 上调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24604931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com