运行第二个Observable是第一个Observable是false
Run a second Observable is first Observable is false
我在 Angular 9 中创建了以下 AuthenticatedGuard:
export class AuthenticatedGuard implements CanActivate {
constructor(private authenticationService: AuthenticationService) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.authenticationService.isSignedIn();
this.authenticationService.signInRedirect({ state: { url: state.url }}).subscribe();
}
}
方法 authenticationService.isSignedIn
returns Observable<boolean>
和 authenticationService.signInRedirect()
returns Observable<any>
.
行为应该是:
如果 authenticationService.isSignedIn
是 false
则执行:
this.authenticationService.signInRedirect({ state: { url: state.url }}).subscribe();
可以这样做并仍然使用 Observable 吗?
将保护更改为:-
export class AuthenticatedGuard implements CanActivate {
constructor(private authenticationService: AuthenticationService) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.authenticationService.isSignedIn().pipe(tap((res)=> {
if(!res) {
this.authenticationService.signInRedirect({ state: { url: state.url }}).subscribe();
}
}));;
}
}
您可以 调用 isSignedIn()
然后执行 signInRedirect()
重定向,我们还需要 return 一个 true/false
值。
async canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
const isSignedIn = awiat this.authenticationService.isSignedIn().first().toPromise();
if (!isSignedIn) {
this.authenticationService.signInRedirect({ state: { url: state.url }}).subscribe();
return false;
}
return true;
}
我在 Angular 9 中创建了以下 AuthenticatedGuard:
export class AuthenticatedGuard implements CanActivate {
constructor(private authenticationService: AuthenticationService) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.authenticationService.isSignedIn();
this.authenticationService.signInRedirect({ state: { url: state.url }}).subscribe();
}
}
方法 authenticationService.isSignedIn
returns Observable<boolean>
和 authenticationService.signInRedirect()
returns Observable<any>
.
行为应该是:
如果 authenticationService.isSignedIn
是 false
则执行:
this.authenticationService.signInRedirect({ state: { url: state.url }}).subscribe();
可以这样做并仍然使用 Observable 吗?
将保护更改为:-
export class AuthenticatedGuard implements CanActivate {
constructor(private authenticationService: AuthenticationService) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.authenticationService.isSignedIn().pipe(tap((res)=> {
if(!res) {
this.authenticationService.signInRedirect({ state: { url: state.url }}).subscribe();
}
}));;
}
}
您可以 isSignedIn()
然后执行 signInRedirect()
重定向,我们还需要 return 一个 true/false
值。
async canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
const isSignedIn = awiat this.authenticationService.isSignedIn().first().toPromise();
if (!isSignedIn) {
this.authenticationService.signInRedirect({ state: { url: state.url }}).subscribe();
return false;
}
return true;
}