AuthService的Grunt缩小失败

时间:2015-08-22 10:49:22

标签: angularjs bundling-and-minification grunt-contrib-uglify

注意:请参阅EDIT 2较短的问题示例

这是我的AuthService缩小代码:

var Services;
!function(a) {
    var b = function() {
        function a(a, b, c) {
            var d = this;
            d.$q = a, d.$http = b, d.$rootScope = c;
        }
        return a.prototype.login = function(a) {
        //....
        }, a.AuthServiceFactory = function(b, c, d) {
            return new a(b, c, d);
        }, a;
    }();
    a.AuthService = b, b.$inject = [ "$q", "$http", "$rootScope" ], angular.module("eucngts").factory("AuthService", b.AuthServiceFactory);
}(Services || (Services = {}));

这是我的控制器缩小代码,我收到错误:

var Controllers;
!function(a) {
    var b = function() {
        function a(a, b, c, d, e, f) {
        //...
        }
        //...
    }();
    a.HeaderController = b, b.$inject = [ "$scope", "$location", "AuthService", "$rootScope", "$modal", "$timeout" ], 
    angular.module("eucngts").controller("HeaderController", b);
}(Controllers || (Controllers = {}));

错误信息是:

Unknown provider: bProvider <- b <- AuthService

否则当我评论使用此服务的行时,我的其他控制器和服务工作正常。

源文件中的行顺序似乎很好

我的连接和非缩小的js文件也可以正常工作。

我可能有理由得到这个错误。

修改

当我没有使用uglified代码而不是uglified代码时,它可以正常工作

var Services;
(function (Services) {
    var AuthService = (function () {
        function AuthService($q, $http, $rootScope) {
            var self = this;
            self.$q = $q;
            self.$http = $http;
            self.$rootScope = $rootScope;
        }
        //...
        AuthService.AuthServiceFactory = function ($q, $http, $rootScope) {
            return new AuthService($q, $http, $rootScope);
        };
        return AuthService;
    })();
    Services.AuthService = AuthService;
    AuthService.$inject = ['$q', '$http', '$rootScope'];
    angular.module('eucngts').factory('AuthService', AuthService.AuthServiceFactory);
})(Services || (Services = {}));

编辑2: 这是另一项服务的完整代码,我无法定义更短,更容易检查的服务:

var Services;

!function(a) {
    var xxx = function() {
        function a(a, zzz) {
            var c = this;
            this.request = function(a) {
                a.headers = a.headers || {};
                var zzz = JSON.parse(localStorage.getItem("authorizationData"));
                return zzz && (a.headers.Authorization = "Bearer " + zzz.token), a;
            }, this.responseError = function(a) {
                var zzz = c;
                return 401 === a.status && (localStorage.removeItem("authorizationData"), zzz.$location.path("/login")), 
                zzz.$q.reject(a);
            };
            var d = this;
            d.$location = a, d.$q = zzz;
        }
        return a.AuthInterceptorServiceFactory = function(kkk, c) {
            return new a(kkk, c);
        }, a;
    }();
    a.AuthInterceptorService = xxx, xxx.$inject = [ "$location", "$q" ], angular.module("eucngts").factory("AuthInterceptorService", xxx.AuthInterceptorServiceFactory);
}(Services || (Services = {}));

none-uglified code:

var Services;
(function (Services) {
    var AuthInterceptorService = (function () {
        function AuthInterceptorService($location, $q) {
            var _this = this;
            this.request = function (config) {
                var self = _this;
                config.headers = config.headers || {};
                var authData = JSON.parse(localStorage.getItem('authorizationData'));
                if (authData) {
                    config.headers.Authorization = 'Bearer ' + authData.token;
                }
                return config;
            };
            this.responseError = function (rejection) {
                var self = _this;
                if (rejection.status === 401) {
                    localStorage.removeItem('authorizationData');
                    self.$location.path('/login');
                }
                return self.$q.reject(rejection);
            };
            var self = this;
            self.$location = $location;
            self.$q = $q;
        }
        AuthInterceptorService.AuthInterceptorServiceFactory = function ($location, $q) {
            return new AuthInterceptorService($location, $q);
        };
        return AuthInterceptorService;
    })();
    Services.AuthInterceptorService = AuthInterceptorService;
    AuthInterceptorService.$inject = ['$location', '$q'];
    angular.module('eucngts').factory('AuthInterceptorService', AuthInterceptorService.AuthInterceptorServiceFactory);
})(Services || (Services = {}));

说:

Unknown provider: kkkProvider <- kkk <- AuthInterceptorService <- $http <- $templateRequest <- $compile

1 个答案:

答案 0 :(得分:0)

以下是我在@YOU

的帮助下回答我的问题

AuthInterceptorService

的类型代码
module Services {
    export class AuthInterceptorService {
        $location: ng.ILocationService;
        $q: ng.IQService;
        constructor($location, $q) {
            var self = this;
            self.$location = $location;
            self.$q = $q;
        }
        //...
        static AuthInterceptorServiceFactory($location, $q) {   
            return new AuthInterceptorService($location, $q);
        }
    }
    AuthInterceptorService.$inject = ['$location', '$q'];
    angular.module('eucngts').factory('AuthInterceptorService', AuthInterceptorService.AuthInterceptorServiceFactory);   
}

当我将其更新为:

module Services {
    export class AuthInterceptorService {
        $location: ng.ILocationService;
        $q: ng.IQService;
        constructor($location, $q) {
            var self = this;
            self.$location = $location;
            self.$q = $q;
        }
        //..
        static AuthInterceptorServiceFactory($location, $q) {   
            return new AuthInterceptorService($location, $q);
        }
    }
    AuthInterceptorService.AuthInterceptorServiceFactory.$inject = ['$location', '$q'];
    angular.module('eucngts').factory('AuthInterceptorService', AuthInterceptorService.AuthInterceptorServiceFactory);
}

按预期工作