AngularJS$http.interceptors.push未捕获错误:[$injector:unpr]

AngularJS$http.interceptors.push未捕获错误:[$injector:unpr],angularjs,Angularjs,我正在尝试在登录成功后将访问令牌设置为标头。我试图通过使用拦截器来实现它,但出现以下错误: Uncaught Error: [$injector:unpr] Unknown provider: aProvider <- a <- TokenInterceptor <- $http <- $compile 当您丑化源代码时,TokenInterceptor被解析为a,这在角度上下文中找不到 您可以使用在uglify之前预处理您的源代码,它将自动转换您的源代码以使用显式注释

我正在尝试在登录成功后将访问令牌设置为标头。我试图通过使用拦截器来实现它,但出现以下错误:

Uncaught Error: [$injector:unpr] Unknown provider: aProvider <- a <- TokenInterceptor <- $http <- $compile

当您丑化源代码时,
TokenInterceptor
被解析为
a
,这在角度上下文中找不到

您可以使用在uglify之前预处理您的源代码,它将自动转换您的源代码以使用显式注释(数组)样式
myApp.factory('TokenInterceptor',['$q','$window','$location','AuthenticationService',function($q,$window,$location,AuthenticationService){…}])
以缩小安全性


ng annotate
grunt
gulp
插件

看起来问题是由缩小版本引起的,您是否在丑化之前使用
ng annotate
处理源代码?
myApp.config(['$httpProvider',function ($httpProvider) {
   $httpProvider.interceptors.push('TokenInterceptor');
}]);

myApp.factory('TokenInterceptor', function ($q, $window, $location, AuthenticationService) {
    return {
        request: function (config) {
            config.headers = config.headers || {};
            if ($window.sessionStorage.token) {
                config.headers.Authorization = 'Bearer ' + $window.sessionStorage.token;
            }
            return config;
        },

        requestError: function(rejection) {
            return $q.reject(rejection);
        },

        response: function (response) {
            return response || $q.when(response);
        },

        //Revoke client authentication if 401 is received

        responseError: function(rejection) {
            console.log("Rejecton !");
            console.log(rejection);

            if (rejection != null && rejection.status === 401 && ($window.sessionStorage.token || AuthenticationService.isLogged)) {
                console.log("Revoked !");
                delete $window.sessionStorage.token;
                AuthenticationService.isLogged = false;
                $location.path("/admin/login");
            }

            return $q.reject(rejection);
        }
    };
});
yApp.config(['$httpProvider', function ($httpProvider) {

    var interceptor = ['$q', '$window', '$location', '$injector', function($q, $window, $location, $injector) {

        return {
            request: function (config) {
                config.headers = config.headers || {};
                if ($window.sessionStorage.token) {
                    config.headers.Authorization = 'Bearer ' + $window.sessionStorage.token;
                }
                return config;
            },

            requestError: function(rejection) {
                return $q.reject(rejection);
            },

            response: function (response) {
                return response || $q.when(response);
            },

            // Revoke client authentication if 401 is received

            responseError: function(rejection) {
                console.log(rejection);
                // Dynamically get the service since they can't be injected into config
                var AuthenticationService = $injector.get('AuthenticationService');

                if (rejection != null && rejection.status === 401 && ($window.sessionStorage.token || AuthenticationService.isLogged)) {
                    delete $window.sessionStorage.token;
                    AuthenticationService.isLogged = false;
                    $location.path("/login");
                }

                return $q.reject(rejection);
            }
        };
    }];

    $httpProvider.interceptors.push(interceptor);
}]);