缩小角度js后的未知提供者

时间:2017-09-17 16:33:20

标签: angularjs

我已经创建了角度应用程序,我想在生产中部署它我使用gulp-uglyfy缩小整个角度应用程序在缩小整个应用程序后我得到了authInterceptor的未知提供程序的错误这里是我的authInterceptor

(function () {
'use strict';
angular.module('competitiveMain', [
    "ui.router",
    "ui.bootstrap",
    "oc.lazyLoad",
    "ngSanitize",
    "ngCookies",
    "pascalprecht.translate",
    "ngStorage",
    "timer",
    "competitiveAdmin",
    "smart-table",
    "toastr"
]).factory('authInterceptor', function ($rootScope, $q, $cookieStore, $location) {
    return {
        // Add authorization token to headers
        request: function (config) {
            config.headers = config.headers || {};
            if ($cookieStore.get('user')) {
                config.headers.Authorization = $cookieStore.get('user').token;
            }
            return config;
        },

        // Intercept 401s and redirect you to login
        responseError: function (response) {
            if (response.status === 401) {
                $location.path('/');
                // remove any stale tokens
                $cookieStore.remove('user');
                return $q.reject(response);
            }
            else {
                return $q.reject(response);
            }
        }
    };
})
    .config(['$httpProvider', function ($httpProvider) {
        //Http Interceptor to check auth failures for xhr requests
        $httpProvider.interceptors.push('authInterceptor');
    }]);

})();

有人可以告诉我为什么在缩小js之后会发生这种情况吗?

2 个答案:

答案 0 :(得分:1)

以下是AngularJS风格指南的链接,并解释了为什么会发生这种情况。

https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#manual-annotating-for-dependency-injection

所以将工厂改为:

.factory('authInterceptor', authInterceptor);

authInterceptor.$inject = ["$rootScope", "$q", "$cookieStore", "$location"];

function authInterceptor($rootScope, $q, $cookieStore, $location)
{
    ...
}

答案 1 :(得分:1)

这是一个常见问题,请务必确保在传递依赖关系名称为 strings 的依赖项时,以便Angular知道缩小后要注入的内容

按如下方式更改工厂代码,

.factory('authInterceptor', ['$rootScope', '$q','$cookieStore','$location', function ($rootScope, $q, $cookieStore, $location) {
}]);