【问题标题】:Toast before/during window.location.href在 window.location.href 之前/期间吐司
【发布时间】:2018-06-25 14:34:38
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: angularjs angular-toastr


    【解决方案1】:

    使用定位服务重新路由请求,因为我假设您使用的是 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');
    }]);
    

    【讨论】:

    • 给我类型错误:无法设置未定义的属性“href”
    • @Miguel-Ángel 我编辑了答案以删除哈希,您需要使其与您在 routeProvider 中定义的路线相匹配。我认为它可能是'/login'
    • $location.url('/login');给我 angular.js:14700 TypeError: Cannot read property 'url' of undefined。我正在使用 angularjs 1.6.6。感谢您的帮助
    • @Miguel-Ángel 确保您在这一行正确注入它 $provide.factory('unauthorisedInterceptor', ['$q', '$location', function ($q, $location ) {
    猜你喜欢
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    相关资源
    最近更新 更多