gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:32:53 25 4
gpt4 key购买 nike

我有这个拦截器:

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 变量,因此它可以在函数的参数中重用相同的变量名。这有效地“隐藏”了另一个变量,使其无法从该函数内访问,但由于您没有在那里使用该变量,所以这无关紧要。

换句话说,缩小不应影响您的结果。它似乎是有效的,您将不得不寻找其他地方来弄清楚为什么您没有获得所需的行为。

如果你想看到使用不同变量名的缩小代码,你可以尝试通过在回调方法:

angular.noop(promise);

关于javascript - 为什么我的 Angular 拦截器没有正确缩小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33084483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com