简体   繁体   English

使用grunt uglify进行Angularjs缩小导致js错误

[英]Angularjs minification using grunt uglify resulting in js error

In angularjs we pass parameters as dependency injection. 在angularjs中,我们将参数作为依赖注入传递。 For example, 例如,

function checkInCtrl ($scope, $rootScope, $location, $http){
…..
….
}

So when it gets minified, it becomes like, 所以当它变得糜烂时,就会变得像

function checkInCtrl(a,b,c,d){
}

Now a,b,c,d won't be interpreted as $scope, $rootScope, $location, $http respectively by angular and whole code fails to work. 现在a,b,c,d将不会被解释为$ scope,$ rootScope,$ location,$ http分别由angular和整个代码无法工作。 For this angularjs has provided one solution, which is 为此,angularjs提供了一种解决方案,即

checkInCtrl.$inject = ['$scope', '$rootScope', $location', '$http'];

we can inject different dependencies by using above syntax. 我们可以使用上面的语法注入不同的依赖项。 This worked well till I didn't use some custom angular service as dependency. 这很好用,直到我没有使用一些自定义角度服务作为依赖。 So for example , 所以,例如,

if I have something like 如果我有类似的东西

function checkInCtrl ($scope, $rootScope, $location, $http){
…..
….
}

It works with given solution, but if I have something like 它适用于给定的解决方案,但如果我有类似的东西

function checkInCtrl ($scope, $rootScope, $location, $http, customService){
…..
….
}

Where customService is something like customService就是这样的

angular.module(customService, ['ngResource'])
                .factory('abc', function($resource) {
                                return $resource('/abc');
                })

It's minified version doesn't get interpreted properly by angular. 它的缩小版本无法通过角度正确解释。

As we had to start project development activities, we couldn't spend enough time to look into matter and we started using controller without minifying them. 由于我们必须开始项目开发活动,我们无法花足够的时间来研究问题,我们开始使用控制器而不缩小它们。 So first question is whether there is such problem with angular or I made some mistake and due to which it didn't work? 所以第一个问题是角度是否存在这样的问题,或者我犯了一些错误,并且由于它不起作用? If such issue exist,what is solution to it? 如果存在这样的问题,它的解决方案是什么?

You have to use the string-injection based syntax that ensure that the minified version points to the good dependancy : 您必须使用基于字符串注入的语法,以确保缩小版本指向良好的依赖性:

function checkInCtrl ($scope, $rootScope, $location, $http){}

becomes : 成为:

['$scope', '$rootScope', '$location', '$http', function checkInCtrl ($scope, $rootScope, $location, $http){}]

For info, ngMin has been deprecated . 有关信息,ngMin已被弃用 You should use ngAnnotate instead which works beautifully with grunt and gulp . 您应该使用ngAnnotate ,而不是与精美的作品咕噜一饮而尽

Navdeep, 纳瓦迪普,

The suggested solution from Bixi will work. 来自Bixi的建议解决方案将起作用。 However the easier way is just to use ngmin Grunt plugin. 然而,更简单的方法就是使用ngmin Grunt插件。 Using this plugin, you don't need to handle the dependency injection like what you did and also no need for the special syntax like Bixi. 使用这个插件,你不需要像你所做的那样处理依赖注入,也不需要像Bixi这样的特殊语法。

To use it, make sure you have grunt-ngmin and that you call it before uglify. 要使用它,请确保你有grunt-ngmin并在grunt-ngmin之前调用它。

Your Gruntfile.js: 你的Gruntfile.js:

ngmin: {
  dist: {
    files: [{
      expand: true,
      cwd: '.tmp/concat/scripts',
      src: '*.js',
      dest: '.tmp/concat/scripts'
    }]
  }
},

....

grunt.registerTask('build', [
  'ngmin',
  'uglify',
]);

Getting uglify and minify to work will reveal (as it did in my case) places where injected variables are changed from something like $scope to 'a' Example: This code: 获得uglify和minify工作将揭示(就像我的情况一样)注入变量从$ scope变为'a'的地方示例:此代码:

controller: function($scope) {
        $scope.showValidation= false;
        this.showValidation = function(){
            $scope.showValidation = true;
        };
    }

after minify and uglify becomes: 缩小后,uglify变为:

controller:function(a){a.showValidation=!1,this.showValidation=function(){a.showValidation=!0}}

And you get an error because 'a' is not the same as $scope. 而且你得到一个错误,因为'a'与$ scope不同。

Solution is to explicitly declare the injected variables: 解决方案是显式声明注入的变量:

controller: ['$scope', function($scope) {
        $scope.showValidation= false;
        this.showValidation = function(){
            $scope.showValidation = true;
        };
    }]

after minify and uglify becomes: 缩小后,uglify变为:

controller:["$scope",function(a){a.showValidation=!1,this.showValidation=function(){a.showValidation=!0}}]

Now 'a' is mapped to $scope. 现在'a'映射到$ scope。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM