[英]Angular 4.3 Interceptor not working
I try to use new Angular 4.3
interceptors for setting authorithation header for all requests. 我尝试使用新的
Angular 4.3
拦截器为所有请求设置authorithation标头。 However, it is not working. 但是,它不起作用。 I set breakpoint into the interceptors
intercept
method and browser did not hit it, so it seems like angular
just ignores my interceptor. 我将断点设置为拦截器
intercept
方法并且浏览器没有击中它,所以看起来像angular
只是忽略了我的拦截器。 Please help me, thanks in advance. 请帮助我,提前谢谢。
user.service.ts: user.service.ts:
import {Injectable} from '@angular/core';
import 'rxjs/add/operator/map';
import {Observable} from "rxjs";
import {Http} from "@angular/http";
@Injectable()
export class UserService {
constructor(public http: Http) {
}
public getContacts(): Observable<any[]> {
return this.http.get('/users/contacts').map(contacts => contacts.json());
}
}
token.interceptor.ts token.interceptor.ts
import {Injectable} from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor
} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {AuthService} from "./shared/auth.service";
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(public auth: AuthService) {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${this.auth.getToken()}`
}
});
return next.handle(request);
}
}
app.module.ts: app.module.ts:
@NgModule({
...
providers: [
...
{provide: HTTP_INTERCEPTORS, useClass: TokenInterceptor, multi: true}
]
})
TL;DR Make sure there is one import of the HttpClientModule in the entire app (recommened) or provide valid interceptor configuration for each one (ofc for testing only). TL; DR确保整个应用程序中有一个HttpClientModule导入(推荐)或为每个导入提供有效的拦截器配置(ofc仅用于测试)。
Make sure that HttpClientModule is not imported multiple times across different modules of the app. 确保不会在应用程序的不同模块中多次导入HttpClientModule。 I had it imported in lazy loaded modules.
我在延迟加载的模块中导入它。 Each import creates a new copy of HttpClient using configuration from the module where it is imported, so interceptors provided in a root module are overwritten.
每次导入都会使用导入模块中的配置创建HttpClient的新副本,因此将覆盖根模块中提供的拦截器。
The reason - you use old Http
service instead of new service, introduced in Angular 4.3
- HttpClient
( Http
is going to be deprecated). 原因 - 您使用旧的
Http
服务而不是新服务,在Angular 4.3
引入 - HttpClient
( Http
将被弃用)。 Also, in the HttpClient
JSON response type is assumed by default, so you should ommit .map(contacts => contacts.json())
. 此外,在
HttpClient
默认采用JSON响应类型,因此您应该省略.map(contacts => contacts.json())
。
app.module.ts app.module.ts
...
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
HttpClientModule,
...
],
providers: [
...
{provide: HTTP_INTERCEPTORS, useClass: TokenInterceptor, multi: true}
]
...
})
user.service.ts user.service.ts
...
import {HttpClient} from "@angular/common/http";
@Injectable()
export class UserService {
constructor(public http: HttpClient) {
}
public getContacts(): Observable<any[]> {
return this.http.get('/users/contacts');
}
}
If you have already check that you are using the HttpClient service and you olny import HttpClientModule once but you still have problems check this: 如果您已经检查过您正在使用HttpClient服务,并且您一次导入HttpClientModule但仍然遇到问题请检查:
If you are using providedIn: 'root' and your service is inside a module. 如果您使用的是提供的:'root',并且您的服务位于模块内。
@Injectable({
providedIn: 'root'
})
export class YourService {
...
}
Make sure that you haven't added the service in the providers array of your module. 确保您尚未在模块的providers数组中添加服务。
@NgModule({
...
providers: [
YourService, // REMOVE THIS LINE
]
})
export class YourModule { }
That will overwrite your interceptors for that service. 这将覆盖该服务的拦截器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.