【发布时间】:2026-02-09 13:45:01
【问题描述】:
到目前为止,我的 SPA 应用程序运行良好。它是使用 AngularJS(+ 其他库)在 JavaScript 中开发的。
现在,我想缩小脚本,我正在测试 yuicompressor 和 Google 的编译器。
一旦我部署脚本的缩小版本并对其进行测试,就会出现错误。
压缩前的 JavaScript 文件为:
var MyApp_Sim_Web = angular.module( 'MyApp_Sim_Web', ['ngRoute' , 'ngSanitize']) ;
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------- $routeProvider ------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
MyApp_Sim_Web.config(function($routeProvider) {
$routeProvider
.when ('/Login', {
templateUrl: 'Pages/Login.html' ,
controller: 'LoginController'
})
.when ('/', {
templateUrl: 'Pages/Login.html' ,
controller: 'LoginController'
})
.when ('/User_Main', {
templateUrl: 'Pages/User_Main.html' ,
controller: 'UserController'
})
.otherwise({ redirectTo: '/' });
});
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------- $IndexController ----------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
MyApp_Sim_Web.filter('Make_Timestamp_Readable', function() {
return function(input) {
var date = new String(input),
year = date[ 0] + date[ 1] +
date[ 2] + date[ 3] ,
month = date[ 4] + date[ 5] ,
day = date[ 6] + date[ 7] ,
hour = date[ 8] + date[ 9] ,
minute = date[10] + date[11] ,
seconds = date[12] + date[13] ;
var reformattedDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + seconds;
var newDate = new Date(reformattedDate);
return newDate;
};
});
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------- $IndexController ----------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
MyApp_Sim_Web.controller('IndexController' , ['$rootScope' , '$scope' , '$log' , '$location' , '$sce' , 'DB_Services' , function( $rootScope , $scope , $log , $location , $sce , DB_Services ) {
// Following declaration is aimed to enable access to DB from any controller.
$rootScope.Handle_DB_Request = function(p_Query , p_Callback) {
DB_Services(p_Query).then(function(d) {
p_Callback(d) ;
});
};
} ]) ;
使用 yuicompressor 的缩小版是:
var MyApp_Sim_Web=angular.module("MyApp_Sim_Web",["ngRoute","ngSanitize"]);MyApp_Sim_Web.config(function(a){a.when("/Login",{templateUrl:"Pages/Login.html",controller:"LoginController"}).when("/",{templateUrl:"Pages/Login.html",controller:"LoginController"}).when("/User_Main",{templateUrl:"Pages/User_Main.html",controller:"UserController"}).otherwise({redirectTo:"/"})});MyApp_Sim_Web.filter("Make_Timestamp_Readable",function(){return function(g){var a=new String(g),e=a[0]+a[1]+a[2]+a[3],d=a[4]+a[5],f=a[6]+a[7],c=a[8]+a[9],b=a[10]+a[11],i=a[12]+a[13];var j=e+"-"+d+"-"+f+" "+c+":"+b+":"+i;var h=new Date(j);return h}});MyApp_Sim_Web.controller("IndexController",["$rootScope","$scope","$log","$location","$sce","DB_Services",function(b,d,e,f,c,a){b.Handle_DB_Request=function(h,g){a(h).then(function(i){g(i)})}}]);
同样使用谷歌的编译器是:
var MyApp_Sim_Web=angular.module("MyApp_Sim_Web",["ngRoute","ngSanitize"]);MyApp_Sim_Web.config(function(a){a.when("/Login",{templateUrl:"Pages/Login.html",controller:"LoginController"}).when("/",{templateUrl:"Pages/Login.html",controller:"LoginController"}).when("/User_Main",{templateUrl:"Pages/User_Main.html",controller:"UserController"}).otherwise({redirectTo:"/"})});
MyApp_Sim_Web.filter("Make_Timestamp_Readable",function(){return function(a){a=new String(a);return new Date(a[0]+a[1]+a[2]+a[3]+"-"+(a[4]+a[5])+"-"+(a[6]+a[7])+" "+(a[8]+a[9])+":"+(a[10]+a[11])+":"+(a[12]+a[13]))}});MyApp_Sim_Web.controller("IndexController",["$rootScope","$scope","$log","$location","$sce","DB_Services",function(a,d,e,f,g,b){a.Handle_DB_Request=function(a,c){b(a).then(function(a){c(a)})}}]);
我得到的错误(Chome 的控制台)是:
[$injector:modulerr] http://errors.angularjs.org/1.4.0-rc.1/$injector/modulerr?p0=PayPlus_Sim_We…2F%2F127.0.0.1%3A59561%2FPublic_Libs%2FAngular%2Fangular.min.js%3A39%3A416)
这很奇怪,因为如上所述,如果没有缩小应用程序可以完美运行(控制台中没有任何类型的错误)。
有人知道发生了什么吗?
提前致谢。
【问题讨论】:
-
MyApp_Sim_Web.config(function($routeProvider) {==>MyApp_Sim_Web.config(['$routeProvider', function($routeProvider) { -
答案提供了解决方案,但没有解释原因。 Angular 的 dep 系统通过解析函数中的
.toString()来破解命名参数。然后它使用函数定义中的物理参数名称来查找 dep。当您缩小它时,它会重命名函数参数,这意味着 Angular 会尝试寻找不存在的依赖项。使用数组表示法将其命名为别名,因此 Angular 通过数组值而不是参数名称查找 deps。所以你在每一个注入东西的地方都应该使用数组表示法。 -
非常感谢@ste2425 的解释。不幸的是,不能投票给评论,否则我会。
标签: javascript angularjs compilation