0

我想在重定向到登录之前显示一个祝酒词。就我而言,我的拦截器中有这段代码:

'use strict';
 angular.module('frontEndApp')
 .config(['$provide', '$httpProvider', function ($provide, $httpProvider, 
 $translate, toastr, CONST) {
    $provide.factory('unauthorisedInterceptor', ['$q', function ($q) {
        return {
            'responseError': function (rejection) {
                if (rejection.status === (401)) {
                    window.location.href = '/#/login';
                }
                if (rejection.status === (405)) {
                    window.location.href = '/#/login';
                    $translate('createsuccess')
                        .then(function (translatedMessage) {
                            toastr.success(translatedMessage, {
                                'timeOut': CONST.TOAST.timeOut,
                                'extendedTImeout': CONST.TOAST.extendedTImeout,
                                'progressBar': CONST.TOAST.progressBar,
                                'closeButton': CONST.TOAST.closeButton,
                                'showMethod': CONST.TOAST.showMethod,
                                'hideMethod': CONST.TOAST.slideUp
                            });
                        });
                }
                return $q.reject(rejection);
            }

        };
    }]);
    $httpProvider.interceptors.push('unauthorisedInterceptor');
}]);

我想向用户展示他们为什么重定向到登录页面...

你能帮帮我吗,烤面包机没有出现。

4

1 回答 1

0

使用位置服务重新路由请求,因为我假设您正在使用 ngRoute。使用 window.location.href 您会导致浏览器执行获取请求并重新加载整个页面,而不是使用 Angular 的路由。

'use strict';
 angular.module('frontEndApp')
 .config(['$provide', '$httpProvider', function ($provide, $httpProvider, 
 $translate, toastr, CONST) {
    $provide.factory('unauthorisedInterceptor', ['$q', '$location', function ($q, $location) {
        return {
            'responseError': function (rejection) {
                if (rejection.status === (401)) {
                    $location.url('/login');
                }
                if (rejection.status === (405)) {
                    $location.url('/login');
                    $translate('createsuccess')
                        .then(function (translatedMessage) {
                            toastr.success(translatedMessage, {
                                'timeOut': CONST.TOAST.timeOut,
                                'extendedTImeout': CONST.TOAST.extendedTImeout,
                                'progressBar': CONST.TOAST.progressBar,
                                'closeButton': CONST.TOAST.closeButton,
                                'showMethod': CONST.TOAST.showMethod,
                                'hideMethod': CONST.TOAST.slideUp
                            });
                        });
                }
                return $q.reject(rejection);
            }

        };
    }]);
    $httpProvider.interceptors.push('unauthorisedInterceptor');
}]);
于 2018-06-25T14:42:40.590 回答