Jér*_*lle 3 rxjs typescript angular
我想将我的 angular 4 迁移到 angular 5。我完成了从 angular 4 到 angular 5 的迁移,现在我的拦截器出现错误以刷新我的令牌。如果出现 401 错误,我将使用此代码拦截所有请求并刷新我的令牌:
import { Observable } from 'rxjs/Observable';
import { Injectable, Injector } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { UserService } from "./user/services/user.service";
import { SpinnerService } from "angular-spinners";
@Injectable()
export class AngularInterceptor implements HttpInterceptor {
public userService;
public spinnerService;
constructor(private injector: Injector) {
}
private applyCredentials = function (req) {
return req.clone({
headers: req.headers.set('Authorization', 'Bearer ' + localStorage.getItem('eb-app-token'))
});
};
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.userService = this.injector.get(UserService);
this.spinnerService = this.injector.get(SpinnerService);
return next.handle(this.applyCredentials(req))
/*.do(event => {
if (event instanceof HttpResponse) {
console.log(event);
}
})*/
.catch((res) => {
console.log('Token refresh');
if (res.status === 401 || res.status === 403) {
return this.userService.refreshToken().first().flatMap((data: any) => {
console.log(data.data);
if (data.data !== '') {
localStorage.removeItem('token');
localStorage.setItem('token', data.data);
this.userService.token = localStorage.getItem('token');
this.userService.isAuthenticated = true;
} else {
this.userService.logOut();
return Observable.throw(res);
}
return next.handle(this.applyCredentials(req));
});
}
else if (res.status === 500) {
this.spinnerService.hide('spinner');
this.spinnerService.hide('spinner-search');
this.userService.logOut();
}
return Observable.throw(res);
});
}
}
Run Code Online (Sandbox Code Playgroud)
我有这个错误:
ERROR in src/app/app.interceptor.ts(25,9): error TS2322: Type 'Observable<{} | HttpProgressEvent | HttpSentEvent | HttpHeaderResponse | HttpResponse<any> | Http...' is not assignable to type 'Observable<HttpEvent<any>>'.
Type '{} | HttpProgressEvent | HttpSentEvent | HttpHeaderResponse | HttpResponse<any> | HttpUserEvent<a...' is not assignable to type 'HttpEvent<any>'.
Type '{}' is not assignable to type 'HttpEvent<any>'.
Type '{}' is not assignable to type 'HttpUserEvent<any>'.
Property 'type' is missing in type '{}'.
Run Code Online (Sandbox Code Playgroud)
我试图修改不同的东西。我读过这篇文章Observable<{}> notassignable to type Observable<SomeType[]>但我没有看到同样的问题......
第 25 行是:
return next.handle(this.applyCredentials(req))
Run Code Online (Sandbox Code Playgroud)
更新
我修改了:
return next.handle(this.applyCredentials(req))
Run Code Online (Sandbox Code Playgroud)
经过
return <any>next.handle(this.applyCredentials(req))
Run Code Online (Sandbox Code Playgroud)
和进口:
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/catch';
Run Code Online (Sandbox Code Playgroud)
您没有将具有预期类型的对象发送到您的句柄函数。您可能可以通过将其解析为 来快速修复它,例如
return next.handle(<any> this.applyCredentials(req))
Run Code Online (Sandbox Code Playgroud)
但是您确实应该检查您的返回类型并确保它符合预期。
更新的答案:
问题实际上出在函数返回值上,应该是Observable<HttpEvent<any>>. 简单的解决方法是投射到any喜欢这个
return <any> next.handle(this.applyCredentials(req))
但是不能保证它会起作用,因为它不属于正确的类。
| 归档时间: |
|
| 查看次数: |
7290 次 |
| 最近记录: |