用户定义的 HttpInterceptor 的 StaticInjectorError 异常
StaticInjectorError exception for user defined HttpInterceptor
在 运行 ng 测试后我得到以下错误。
NullInjectorError: StaticInjectorError[RequestInterceptor]:
NullInjectorError:没有 RequestInterceptor 的提供者!
RequestInterceptor
import { HttpEvent, HttpHandler, HttpHeaders, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
@Injectable()
export class RequestInterceptor implements HttpInterceptor
{
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>
{
let clonedRequest = request.clone();
if(!request.url.endsWith('/login'))
{
const jwtToken = sessionStorage.getItem("Jwt_Token");
clonedRequest = request.clone({
headers: new HttpHeaders({
"Authorization": "Bearer " + jwtToken,
'Content-Type': 'application/json'
})
})
}
return next.handle(clonedRequest.clone());
}
}
请求-interceptor.spec.ts
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';
import { RequestInterceptor} from './request-interceptor';
describe('RequestInterceptorService', () => {
beforeEach(() => TestBed.configureTestingModule({
declarations: [],
imports: [HttpClientModule, FormsModule, RouterTestingModule]
}));
it('should be created', () => {
const service: RequestInterceptor = TestBed.get(RequestInterceptor);
expect(service).toBeTruthy();
});
});
有人可以告诉我为什么会收到这个吗?
只需将 RequestInterceptor
添加到提供商 :
beforeEach(() => TestBed.configureTestingModule({
declarations: [],
providers: [RequestInterceptor],
imports: [HttpClientModule, FormsModule, RouterTestingModule]
}));
在 运行 ng 测试后我得到以下错误。
NullInjectorError: StaticInjectorError[RequestInterceptor]: NullInjectorError:没有 RequestInterceptor 的提供者!
RequestInterceptor
import { HttpEvent, HttpHandler, HttpHeaders, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
@Injectable()
export class RequestInterceptor implements HttpInterceptor
{
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>
{
let clonedRequest = request.clone();
if(!request.url.endsWith('/login'))
{
const jwtToken = sessionStorage.getItem("Jwt_Token");
clonedRequest = request.clone({
headers: new HttpHeaders({
"Authorization": "Bearer " + jwtToken,
'Content-Type': 'application/json'
})
})
}
return next.handle(clonedRequest.clone());
}
}
请求-interceptor.spec.ts
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';
import { RequestInterceptor} from './request-interceptor';
describe('RequestInterceptorService', () => {
beforeEach(() => TestBed.configureTestingModule({
declarations: [],
imports: [HttpClientModule, FormsModule, RouterTestingModule]
}));
it('should be created', () => {
const service: RequestInterceptor = TestBed.get(RequestInterceptor);
expect(service).toBeTruthy();
});
});
有人可以告诉我为什么会收到这个吗?
只需将 RequestInterceptor
添加到提供商 :
beforeEach(() => TestBed.configureTestingModule({
declarations: [],
providers: [RequestInterceptor],
imports: [HttpClientModule, FormsModule, RouterTestingModule]
}));