我正在将我的应用升级到 Angular 6 . 我正在从 Angular 4 升级,但下面的代码在Angular 6中导致错误,它在Angular 4中工作正常 .
我得到的错误:
属性'of'在类型'typeof Observable'上不存在'错误:类型'Observable'上不存在属性'catch'
我该如何解决这些错误?
private authInterceptor(observable: Observable<Response>): Observable<Response> {
return observable.catch((error, source) => {
if (error.status == 401) {
this.router.navigateByUrl('/login');
return Observable.of();
} else {
return Observable.throw(error);
}
});
}
6 回答
由于你标记了你的问题rxjs6,我'm assuming the upgrade to Angular 6 includes an upgrade to rxjs6. In that case, it'不能正常工作,因为可观察对象上的方法现在是可以使用pipe()应用的独立运算符 . 此外,进口也发生了变化 . 有关详细信息,请参阅migration guide .
使用rxjs6它应该看起来像这样:
您将需要导入您正在使用的所有运算符 .
或者以这种方式导入Observable:
需要导入catch操作符
我假设您已迁移到RXJS6,因为您已迁移到angular6 .
在RXJS6中使用catch错误而不是catch如here
使用以下方法导入库并重新排列代码
这对我有用 .