如何在retryWhen重试耗尽后为Angular的canActivate守卫返回值?解决页面刷新空白问题
Fix EmptyError When Retry Fails in canActivate
Got it, let's sort out this EmptyError issue you're facing. The root problem right now is that after exhausting your 3 retries, your Observable doesn't emit any value—it just completes without a result, which makes canActivate throw that annoying EmptyError. The finalize block runs and handles the navigation, but it doesn't send a boolean value into the stream, leaving the Observable empty.
Here's how to adjust your code to ensure we emit false when retries are used up, while still navigating to the no-access area properly:
private getUserData(route: ActivatedRouteSnapshot): Observable<boolean> { return this.userService.getUserData() .pipe( map(userData => { if(userData === null) throw new Error('User Data not initialized'); return userData; }), retryWhen(errors => errors.pipe( delay(1000), take(3), concatMap((error, retryIndex) => { // Check if this is the last allowed retry (0-based index, so 2 = 3rd attempt) if (retryIndex === 2) { this.navigateToNoAccessArea(); return of(false); // Emit false to give canActivate a valid value } return throwError(() => error); // Keep retrying if we haven't hit the limit }) ) ), map(userData => { // Your existing permission check logic here, return true or false // Example: return this.validateUserPermissions(userData, route); }), // Safety net to catch any unexpected errors and return false catchError(() => of(false)) ); }
Why this works:
- Instead of relying on
finalize(which is for cleanup, not stream emissions), we useconcatMapinsideretryWhento track retry attempts. When we hit the 3rd retry, we trigger the navigation and emitfalsedirectly into the stream. - This guarantees the Observable always outputs a boolean value, so
canActivatenever receives an empty sequence that would throw the error. - The final
catchErroracts as a fallback to handle any unforeseen errors and ensure we still return a valid boolean for the guard.
内容的提问来源于stack exchange,提问作者Sandor Szilard
相关产品推荐
相关产品推荐

