此令牌拦截器服务出了什么问题?它显示错误: 属性“ do”在类型“ Observable>”上不存在。 在此行下: 返回next.handle(_req).do((event:HttpEvent)=> {
import { switchMap } from 'rxjs/operators';
import { TokenProvider } from './token/token';
import { Observable } from 'rxjs/Observable';
import { fromPromise } from 'rxjs/observable/fromPromise';
import { Injectable } from '@angular/core';
import {
HttpInterceptor,
HttpRequest,
HttpHandler,
HttpEvent,
HttpResponse,
HttpErrorResponse
} from '@angular/common/http';
import {
IonicPage,
NavController,
NavParams,
AlertController,
LoadingController
} from 'ionic-angular';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(private tokenProvider: TokenProvider, public navCtrl: NavController) {}
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
return fromPromise(this.tokenProvider.GetToken()).pipe(
switchMap(token => {
const headersConfig = {
'Content-Type': 'application/json',
Accept: 'application/json'
};
if (token) {
headersConfig['Authorization'] = `beader ${token}`;
}
const _req = req.clone({ setHeaders: headersConfig });
return next.handle(_req).do((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
// do stuff with response if you want
}
}, (err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
this.navCtrl.push('LoginPage');
...
如果无法解决该错误,那么还有另一个选项可以用“ do”代替吗?