简体   繁体   English

Angular:HTTP拦截器没有被注入配置块

[英]Angular : http Interceptor is not getting injected in config block

I have created a simple http interceptor with which I intend to log the requests and responses made. 我创建了一个简单的http拦截器,打算用来记录请求和响应。 Issue is that it is undefined in config block which gets executed first and throws error because of which module couldn't get instantiated. 问题是它在config块中未定义,该块首先执行并由于无法实例化哪个模块而引发错误。 Below is the code used : 下面是使用的代码:

authApp
.factory("httpInterceptor",function($log){
    var httpInterceptor = {};

    httpInterceptor.request = function(config){
        $log.info("request is being made to " + config.url);
        return config;
    };

    httpInterceptor.response = function(config){
        $log.info("request to URL : " + config.url + " is completed 
        now.");
        return config;
    };

    return httpInterceptor;
})
.config(function($httpProvider){
    // Http Interceptor
    $httpProvider.interceptors.push(httpInterceptor);
 });

Thanks in advance !! 提前致谢 !!

Your code seems correct, except a particular line where you are pushing your brand new interceptor to the list of $httpProvider 's interceptors. 您的代码似乎是正确的,除了要在其中$httpProvider全新的拦截器到$httpProvider的拦截器列表的特定行之外。

$httpProvider.interceptors.push(httpInterceptor);

This line doesn't know what httpInterceptor is because it is defined as an angular factory. 该行不知道什么是httpInterceptor ,因为它被定义为角度工厂。 Change it to 更改为

$httpProvider.interceptors.push('httpInterceptor'); // Note the quotes around it.

and retry. 然后重试。

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

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