从我所看到的一切来看,我有两个拦截器似乎还没有初始化。否则我会看到console.logs写的。我不知道我在做什么错...帮助!!!!
// token.interceptor.ts //这应该将令牌作为参数添加到每个请求中...
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { StorageService } from '../storage.service';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
token: string;
constructor(
private storageService: StorageService
) {
console.log('token interceptor initialized');
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log(this.storageService.tokenString);
if (this.storageService.tokenString) {
const addToken = req.clone({
setParams: {
token: this.storageService.tokenString
}
});
return next.handle(addToken);
} else {
return next.handle(req);
}
}
}
// authentication.interceptor.ts //这应该重定向到登录名以解决401错误。
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { catchError } from 'rxjs/operators';
import { throwError } from 'rxjs';
@Injectable()
export class AuthenticationInterceptor implements HttpInterceptor {
constructor(
private router: Router,
) {
console.log('auth interceptor initialized');
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
catchError((event: HttpEvent<any>) => {
if (event instanceof HttpErrorResponse) {
if (event.status === 401) {
this.router.navigate(['app-login']);
}
}
return throwError(event);
}
)
);
}
}
//,然后在主app.module.ts中提供它们
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { SharedModule } from './shared/shared.module';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AuthGuard } from './gaurds/auth.guard';
import { CoreModule } from './core/core.module';
import { AdminGuard } from './gaurds/admin.guard';
import { ManagerGuard } from './gaurds/manager.guard';
import { StorageService } from './storage.service';
import { AuthenticationInterceptor } from './http/authentication.interceptor';
import { TokenInterceptor } from './http/token.interceptor';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
AppRoutingModule,
BrowserAnimationsModule,
SharedModule,
CoreModule
],
providers: [
AuthGuard,
AdminGuard,
ManagerGuard,
StorageService,
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true,
},
{
provide: HTTP_INTERCEPTORS,
useClass: AuthenticationInterceptor,
multi: true
},
/* {
provide: HTTP_INTERCEPTORS,
useClass: ResponseInterceptor,
multi: true
} */
],
bootstrap: [AppComponent]
})
export class AppModule {}
我首先开始对此有疑问,因为我可以看到token.interceptor.ts没有将令牌参数添加到请求中,因此我将控制台日志放入以开始调试。看到这没有被初始化后,我在第二个拦截器中测试了相同的东西,发现它也没有被初始化。我在做什么错了?