【问题标题】:TypeScript Interceptor in Angularjs Not WorkingAngularjs 中的 TypeScript 拦截器不起作用
【发布时间】:2015-03-05 03:16:26
【问题描述】:

我的拦截器不工作(未触发)。我有什么错误吗?对不起,我还是个菜鸟:(

module myApp {
export class HttpConfigurator {
    public configure(httpProvider: ng.IHttpProvider) {
        console.log(httpProvider);
        console.log(httpProvider.interceptors[0]);

        httpProvider.interceptors.push([
            '$location',
            '$q',
            '$log',
            ($location: ng.ILocationService, $q: ng.IQService, $log: ng.ILogService) => {
                return promise => promise.then(
                    response => { console.log(response.status); return response; },
                    response => {
                        console.log(response.status);
                        if (response.status >= 500) {

                        }

                        if (response.status === 401 || response.status === 403) {
                            $location.path('/403');
                            return $q.reject(response);
                        } else {
                            return $q.reject(response);
                        }
                    });
            }]);
    }
}

}

注意:以前这是在 javascript 中工作的。当我将其转换为打字稿时,问题就开始了。

【问题讨论】:

标签: angularjs typescript interceptor


【解决方案1】:

我在任何地方都没有看到promise 的定义。请注意,由于拦截器的简单性,我只使用函数 + 工厂:

app.factory('appHttpInterceptor', [function () {
    return {
        response: function (response) {
            if (typeof (MiniProfiler) != 'undefined') {
                var ids = response.headers()['x-miniprofiler-ids'];
                if (ids) {
                    MiniProfiler.fetchResultsExposed($.parseJSON(ids));
                }
            }

            return response;
        }
    };
}]);

不要使用class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 2017-09-26
    • 1970-01-01
    相关资源
    最近更新 更多