简体   繁体   English

Angular 5 http拦截器不拦截

[英]Angular 5 http interceptor not intercepting

I've got a correct JWT token stored in local storage and an interceptor that I blatantly copied from a tutorial.我有一个正确的 JWT 令牌存储在本地存储中,还有一个我从教程中公然复制的拦截器。 However, it doesn't intercept and add headers to requests.但是,它不会拦截请求并向请求添加标头。

Here's where I make a request:这是我提出请求的地方:

https://github.com/Marred/Informakcja/blob/master/Informakcja-client/src/app/services/information.service.ts https://github.com/Marred/Informakcja/blob/master/Informakcja-client/src/app/services/information.service.ts

here's the interceptor:这是拦截器:

https://github.com/Marred/Informakcja/blob/master/Informakcja-client/src/app/services/token.interceptor.ts https://github.com/Marred/Informakcja/blob/master/Informakcja-client/src/app/services/token.interceptor.ts

and my appmodule - I'm pretty sure it's correctly imported:和我的 appmodule - 我很确定它已正确导入:

https://github.com/Marred/Informakcja/blob/master/Informakcja-client/src/app/app.module.ts https://github.com/Marred/Informakcja/blob/master/Informakcja-client/src/app/app.module.ts

When I make a request I expect the interceptor to log messages that I specified to console and add the token to header, it doesn't do that though and I've got no idea why :/ I checked the code with some other tutorials found online and didn't see any differences capable of breaking my code.当我提出请求时,我希望拦截器记录我指定到控制台的消息并将令牌添加到标头中,但它并没有这样做,我不知道为什么:/我用其他一些教程检查了代码在线并没有看到任何能够破坏我的代码的差异。 I also don't have enough experience with Angular to debug it properly.我也没有足够的 Angular 经验来正确调试它。

Any help would be much apprecieated.任何帮助将不胜感激。

You are doing couple of things wrongly here:你在这里做错了几件事:

Interceptors are used with HttpClientModule not with HttpModule.拦截器与 HttpClientModule 一起使用,而不是与 HttpModule 一起使用。 You are using HttpModule .您正在使用HttpModule You need to change to HttpClientModule您需要更改为HttpClientModule

  1. Add HttpClientModule in imports array in app.module.tsapp.module.ts导入数组中添加HttpClientModule
  2. Import HttpClient in your authentication.service.ts and take its reference in its constructor.在您的authentication.service.ts导入HttpClient并在其构造函数中引用它。

Refer below code:参考以下代码:

  //app.module.ts
    import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
    .
    .
    .
    @NgModule({
      declarations: [
        AppComponent,
       .
       .
       .
      ],
      imports: [
        BrowserModule,
        .
        .
        .,
        HttpClientModule, //add here
        RouterModule.forRoot([
          { path: '', redirectTo: 'home', pathMatch: 'full' },
          { path: 'home', component: HomeComponent },
          { path: 'login', component: LoginComponent },
          { path: 'register', component: RegisterComponent },
          { path: 'add-information', component: AddInformationComponent },
          { path: '**', redirectTo: 'home' }
        ], { useHash: true })
      ],
      providers: [
        { provide: 'BASE_URL', useValue: 'https://some URL/' },
        UserService,
        InformationService,
        AuthenticationService,
        AlertService,
        {
          provide: HTTP_INTERCEPTORS,
          useClass: TokenInterceptor,
          multi: true
        }
      ],
      bootstrap: [AppComponent]
    })
    export class AppModule { }

and

//information.service.ts and authentication.service.ts

import { Injectable, Inject } from '@angular/core';
import { HttpClient} from '@angular/common/http'; //added import
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

@Injectable()
export class InformationService {

  constructor(private http: HttpClient, @Inject('BASE_URL') private baseUrl: string) { } //made changes in cunstructor

  add(link: string, title: string, description: string) {
    return this.http.post(this.baseUrl + 'api/Information', { link: link, title: title, description: description })
    .map((response: Response) => {
        console.log(response);
    });
  }
}

make similar changes in authentication.service.ts在 authentication.service.ts 中进行类似的更改

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

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