gpt4 book ai didi

javascript - Angular 路由解析 - deferred.reject 不起作用 - Angular 1 + TypeScript

转载 作者:行者123 更新时间:2023-12-03 07:23:10 25 4
gpt4 key购买 nike

以下是我的路由配置

function routes($routeProvider: ng.route.IRouteProvider) {

let accessResolver = ['UserFactory', (UserFactory: any) => {
return UserFactory.isAuthenticated();
}];

//Configuring routes
$routeProvider.when('/', {
templateUrl: '/views/home.html',
controller: 'HomeController',
controllerAs: 'homeCtrl',
resolve: accessResolver
}).when('/login', {
templateUrl: '/views/login.html',
controller: 'LoginController',
controllerAs: 'loginCtrl'
}).otherwise({
redirectTo: '/'
});
}

以及我的路线更改错误处理程序

function run($rootScope: ng.IRootScopeService, $location: ng.ILocationService) {
$rootScope.$on("$routeChangeError", () => {
console.log("Route Change Error");
$location.url('/login?redirect=' + $location.url());
});
}

和 UserFactory

module TheHub {
export interface IUserFactory {
isAuthenticated(): ng.IDeferred<String>;
}

class UserFactory implements IUserFactory {

constructor(private $http: ng.IHttpService, private $q: ng.IQService, private $rootScope: any) {
}

isAuthenticated(): ng.IDeferred<String> {
let deferred = this.$q.defer();
if (this.$rootScope.auth && this.$rootScope.auth.isAuthenticationChecked) {
if (this.$rootScope.auth.isAuthenticated) {
deferred.resolve('OK');
} else {
deferred.reject('Unauthorized');
}
} else {
this.$http.get('secure/user').then(
(response: ng.IHttpPromiseCallbackArg<{}>) => {
if (!this.$rootScope.auth) {
this.$rootScope.auth = {};
}
this.$rootScope.auth.isAuthenticationChecked = true;
this.$rootScope.auth.isAuthenticated = true;
deferred.resolve('OK');
},
(error: any) => {
if (!this.$rootScope.auth) {
this.$rootScope.auth = {};
}
this.$rootScope.auth.isAuthenticationChecked = true;
deferred.reject('Unauthorized');
});
}
return deferred;
}
}

function userFactory($http: ng.IHttpService, $q: ng.IQService, $rootScope: any) {
return new UserFactory($http, $q, $rootScope);
}

userFactory.$inject = ['$http', '$q', '$rootScope'];

angular.module('TheHub').factory('UserFactory', userFactory);
}

这里的逻辑是,我正在触发一个请求来检查用户是否已经登录并拥有 session 。问题是,当用户尚未登录时,服务就会失败并且 promise 会被拒绝。但是,我不知道为什么,处理程序 $routeChangeError 没有被触发。当 JavaScript 发生错误时它工作正常。

最佳答案

您忘记了 .promise,因此您只返回了 deferred,它既不是等待的,也不是您期望的分辨率值。

但你应该avoid the deferred antipattern anyway - 就这么做

isAuthenticated(): ng.IPromise<String> {
if (this.$rootScope.auth && this.$rootScope.auth.isAuthenticationChecked) {
if (this.$rootScope.auth.isAuthenticated) {
return this.$q.resolve('OK');
// ^^^^^^^^^^^^^^^^^^^^^^
} else {
return this.$q.reject('Unauthorized');
// ^^^^^^^^^^^^^^^^^^^^^^
}
} else {
return this.$http.get('secure/user').then(
// ^^^^^^
(response: ng.IHttpPromiseCallbackArg<{}>) => {
if (!this.$rootScope.auth) {
this.$rootScope.auth = {};
}
this.$rootScope.auth.isAuthenticationChecked = true;
this.$rootScope.auth.isAuthenticated = true;
return 'OK';
// ^^^^^^
},
(error: any) => {
if (!this.$rootScope.auth) {
this.$rootScope.auth = {};
}
this.$rootScope.auth.isAuthenticationChecked = true;
return this.$q.reject('Unauthorized');
// ^^^^^^^^^^^^^^^^^^^^^
}
);
}
}

关于javascript - Angular 路由解析 - deferred.reject 不起作用 - Angular 1 + TypeScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36115176/

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