简体   繁体   English

HttpInterceptor中的注入服务未初始化

[英]Injected Service in HttpInterceptor not initialized

I am trying to inject a service into an HttpInterceptor, here is the simple service 我试图将服务注入HttpInterceptor,这是简单的服务

import { Injectable } from '@angular/core';
@Injectable()
export class LoginLoadingService {
   constructor() { }
   public loaded: boolean;
   isLoaded() {
       if (this.loaded === undefined) {
             this.loaded = true;
       }
       return this.loaded;
  }    
}

And the interceptor 和拦截器

import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from 
'@angular/common/http';
import { Injectable, Inject } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { LoginLoadingService } from './loading.service';
import 'rxjs/add/operator/do';
@Injectable()
export class LoginLoadingInterceptor implements HttpInterceptor {
  constructor(public loginLoadingService: LoginLoadingService) { }
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    this.loginLoadingService.loaded = false
    return next.handle(req).do((event: HttpEvent<any>) => {
      this.loginLoadingService.loaded = true;
    }, error => console.error(error));
  }
}

And in my app.module 在我的app.module中

providers: [
  LoginLoadingService,
  {
    provide: HTTP_INTERCEPTORS,
    useClass: LoginLoadingInterceptor,
    multi: true
  }
]

The HttpInterceptor is firing fine, but the loginLoadingService is not defined in the intercept() method. HttpInterceptor正常运行,但是在intercept()方法中没有定义loginLoadingService。 I tried to add deps to the interceptor in app.module, but that gave me this error 我试图在app.module中向拦截器添加deps,但这给了我这个错误

params.map is not a function params.map不是一个函数

Not sure what the issue is here 不知道这里的问题是什么

Define dependencies for interceptors like below: 为拦截器定义依赖关系,如下所示:

for example, we have AuthInterceptor and we have injected LocalStorageService and SessionStorageService . 例如,我们有AuthInterceptor ,我们注入了LocalStorageServiceSessionStorageService

in app.module.ts we add interceptor AuthInterceptor : 在app.module.ts中我们添加了拦截器AuthInterceptor

   {
        provide: HTTP_INTERCEPTORS,
        useClass: AuthInterceptor,
        multi: true,
        deps: [LocalStorageService, SessionStorageService]
    },

This will work. 这会奏效。

The answer provided by @Hadi is the correct one.Thought I will add to this. @Hadi提供的答案是正确的。我想补充一点。 The reason behind using deps in app module is explained in details in this article. 本文详细介绍了在app模块中使用deps的原因。 https://blog.thoughtram.io/angular/2015/05/18/dependency-injection-in-angular-2.html#new-staticinjector-apis https://blog.thoughtram.io/angular/2015/05/18/dependency-injection-in-angular-2.html#new-staticinjector-apis

Newer versions of Angular make use of StaticInjector where the DI framework of angular comes into play. 较新版本的Angular使用StaticInjector,其中角度的DI框架发挥作用。 We basically ask the Angular to provide us the singleton instance of a service when required by just injecting them as providers in app.module. 我们基本上要求Angular在需要时通过在app.module中将它们作为提供者注入来为我们提供服务的单例实例。

This is nothing but a shorthand version of {provide: token_name, useClass:class_name} 这只是{provide:token_name,useClass:class_name}的简写版本

So we are requesting angular to inject a token of type (class) using the above code. 所以我们要求angular使用上面的代码注入一个类型(类)的标记。 Now if we need the token to be injected with existing service dependencies then we ask the token to be injected like this 现在,如果我们需要使用现有服务依赖项注入令牌,那么我们要求令牌像这样注入

{provide: token_name, useClass:class_name, deps: [deps_name]} {provide:token_name,useClass:class_name,deps:[deps_name]}

And that is why,the below resolves to the static injection of the interceptor 这就是为什么,下面解决了拦截器的静态注入

{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true, deps: [LocalStorageService, SessionStorageService] }, {provide:HTTP_INTERCEPTORS,useClass:AuthInterceptor,multi:true,deps:[LocalStorageService,SessionStorageService]},

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

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