gpt4 book ai didi

angular - 输入 'Observable<{} is not assignable to type ' Observable>'

转载 作者:行者123 更新时间:2023-12-04 01:40:33 34 4
gpt4 key购买 nike

我想将我的 angular 4 迁移到 angular 5。我完成了从 angular 4 到 angular 5 的迁移,现在我的拦截器出现错误以刷新我的 token 。如果出现 401 错误,我将使用此代码拦截所有请求并刷新我的 token :

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);
});
}
}

我有这个错误:
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 '{}'.

我试图修改不同的东西。我读了这篇文章 Observable<{}> not assignable to type Observable<SomeType[]>但我没有看到同样的问题......

第 25 行是:
return next.handle(this.applyCredentials(req))

UPDATE



我修改了:
return next.handle(this.applyCredentials(req))

经过
return <any>next.handle(this.applyCredentials(req))

和进口:
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/catch';

最佳答案

您没有将具有预期类型的​​对象发送到您的句柄函数。
您可能可以通过将其解析为 来快速修复它,例如

return next.handle(<any> this.applyCredentials(req))

但是您确实应该检查您的返回类型并确保它符合预期。

更新的答案:
问题实际上出在函数返回值上,应该是 Observable<HttpEvent<any>> .
简单的解决方法是强制转换为 any像这样
return <any> next.handle(this.applyCredentials(req))
但是不能保证它会起作用,因为它不属于正确的类。

关于angular - 输入 'Observable<{} is not assignable to type ' Observable<HttpEvent<any>>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47155879/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com