簡體   English   中英

角度路由解析-deferred.reject不起作用-角度1 + TypeScript

[英]Angular route resolution - deferred.reject not working - Angular 1 + TypeScript

以下是我的路線配置

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);
}

這里的邏輯是,我觸發一個請求以檢查用戶是否已經登錄並具有會話。 問題是,當用戶尚未登錄時,服務將失敗,並且承諾將被拒絕。 但是,我不確定為什么不觸發處理程序$ routeChangeError。 出現JavaScript錯誤時,它工作正常。

您已經忘記了.promise因此您只返回了延遲的.promise ,既沒有等待,也沒有期望的分辨率值。

但是無論如何,您都應該避免使用延遲的反模式 -只需這樣做

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');
//              ^^^^^^^^^^^^^^^^^^^^^
            }
        );
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM