Angular 防止Observable在服务器关闭时重试
在下面的代码中,我使用Angular 防止Observable在服务器关闭时重试,angular,rxjs,observable,Angular,Rxjs,Observable,在下面的代码中,我使用httpClient从端点获取用户列表, 在一个完美的世界中,应用程序发送一个http请求并获取用户列表, 但当服务器返回4xx或5xx错误时,应用程序会在10毫秒后重试,并继续向服务器发送数千个http请求 this.http.get<User[]>(SERVER_API_URL + '/api/not_found') .subscribe( value => console.log('subscribe.next'),
httpClient
从端点获取用户列表,
在一个完美的世界中,应用程序发送一个http请求并获取用户列表,
但当服务器返回4xx
或5xx
错误时,应用程序会在10毫秒后重试,并继续向服务器发送数千个http请求
this.http.get<User[]>(SERVER_API_URL + '/api/not_found')
.subscribe(
value => console.log('subscribe.next'),
err => console.log('subscribe.error'),
() => console.log('subscribe.done')
);
this.http.get(SERVER\u API\u URL+'/API/not\u found')
.订阅(
value=>console.log('subscribe.next'),
err=>console.log('subscribe.error'),
()=>console.log('subscribe.done')
);
如何阻止应用程序重试
我试图将可观察的
转换为承诺
,但得到了相同的结果
- “@angular/core”:“6.0.0”
- “@angular/http”:“6.0.5”
- “rxjs”:“6.2.1”
this.http.get<User[]>(SERVER_API_URL + '/api/not_found')
.pipe(
retry(0), // retry a failed request up to 0 times
catchError(this.handleError) // then handle the error
).subscribe(...);
}
this.http.get(SERVER\u API\u URL+'/API/not\u found')
.烟斗(
重试(0),//重试失败的请求最多0次
catchError(this.handleError)//然后处理错误
).认购(……);
}
参考资料:您也可以取消订阅
const sub = this.http.get<User[]>(SERVER_API_URL + '/api/not_found')
.subscribe(
value => {
console.log('subscribe.next')
sub.unsubscribe();
},
err => {
console.log('subscribe.error');
sub.unsubscribe();
},
() => console.log('subscribe.done')
);
const sub=this.http.get(SERVER\u API\u URL+'/API/not\u found')
.订阅(
值=>{
console.log('subscribe.next')
sub.取消订阅();
},
错误=>{
console.log('subscribe.error');
sub.取消订阅();
},
()=>console.log('subscribe.done')
);
这不是可观察对象所做的,因此问题将出现在代码的其他地方。你是对的,我的一个HTTPInterceptor中有一个bug