为什么我的 Angular 拦截器没有正确缩小?

Why is my Angular interceptor not properly minified?

我有这个拦截器:

angular.module('dwExceptionHandler', [])
    .factory('ExceptionInterceptor', ['$q', function ($q) {
        return function (promise) {
            return promise.then(
                function (response) {
                    if (response && response.data.Exception) {
                        alert(response.data.Exception.Message);

                        return $q.reject(response);
                    }

                    return response;
                },
                function (response) {
                    if (response.data.Exception) {
                        console.warn('ALERT', response.data.Exception.Message);
                        alert(response.data.Exception.Message.replace(/\n/g, '\n'));
                    }

                    return $q.reject(response);
                });
        };
    }]).config(["$httpProvider", function ($httpProvider) {
        $httpProvider.responseInterceptors.push('ExceptionInterceptor');
    }]);

我期待收到警报,因为我从服务器返回异常,但未显示警报。它确实出现在我的开发环境中,我没有在其中使用缩小。

当我复制缩小代码的缩小部分时,它看起来像这样:

angular.module("dwExceptionHandler",[]).factory("ExceptionInterceptor",["$q",function(n)
{
    return function(t){
        return t.then(function(t)
            {
                return 
                    t && t.data.Exception 
                        ? (alert(t.data.Exception.Message),n.reject(t))
                        : t
            },
            function(t)
            {
                return t.data.Exception 
                        && 
                        (   console.warn("ALERT",t.data.Exception.Message),
                            alert(t.data.Exception.Message.replace(/\n/g,"\n"))),
                            n.reject(t)
            })
        }}])

        .config(["$httpProvider",function(n){n.responseInterceptors.push("ExceptionInterceptor")}]);

当我研究这段代码时,我注意到 promiseresponse 变量在缩小时都分配了相同的变量:t.

这怎么可能,我该如何解决?我使用 .net Bundle 配置,但不允许切换到 grunt、gulp 或使用 web essentials。我必须坚持使用 Bundle 配置。

尽管两个变量名相同,但它们实际上并没有使用同一个变量。缩小器能够看到您没有在错误回调中使用 promise 变量,因此它可以在函数的参数中重用相同的变量名。这实际上 "hides" 另一个变量,使其无法从该函数内访问,但由于您没有在那里使用该变量,所以这无关紧要。

换句话说,缩小应该不会影响您的结果。它似乎是有效的,您将不得不去别处寻找您没有得到所需行为的原因。

如果你想看到使用不同变量名的缩小代码,你可以尝试通过在回调中添加这样一行来欺骗缩小器认为你在​​那里使用 promise 变量方法:

angular.noop(promise);