Angular 6+ Guards - Return true/false 合并最新订阅
Angular 6+ Guards - Return true/false to combineLatest subscription
我怎样才能在subscribe
里面returntrue/false?
import {Injectable} from '@angular/core';
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {AppState} from '../../app.state';
import {select, Store} from '@ngrx/store';
import {merge, combineLatest} from 'rxjs';
import {map, take, tap} from 'rxjs/operators';
import {HttpClient} from '@angular/common/http';
@Injectable()
export class PermissionsGuard implements CanActivate {
user;
constructor(
private router: Router,
private store: Store<AppState>
) {
}
canActivate(route: ActivatedRouteSnapshot): any {
const academy$ = this.store.pipe(select(state => state.academy));
const user$ = this.store.pipe(select(state => state.user));
return combineLatest(
academy$,
user$
).pipe(
map((data) => {
console.log(data[0]);
console.log(data[1]);
return data;
})
).subscribe((data) => {
console.log(data);
// return true or false to guard
})
}
}
您只是没有订阅您的 observable。
你应该总是 return 下一个链操作符的 Observable,在这种情况下它是守卫。
删除 subscribe((data) => {...})
部分。
如果您必须应用自定义逻辑,请在 map
运算符和 return 中执行任何您想要的 return 即 true
或 false
我怎样才能在subscribe
里面returntrue/false?
import {Injectable} from '@angular/core';
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {AppState} from '../../app.state';
import {select, Store} from '@ngrx/store';
import {merge, combineLatest} from 'rxjs';
import {map, take, tap} from 'rxjs/operators';
import {HttpClient} from '@angular/common/http';
@Injectable()
export class PermissionsGuard implements CanActivate {
user;
constructor(
private router: Router,
private store: Store<AppState>
) {
}
canActivate(route: ActivatedRouteSnapshot): any {
const academy$ = this.store.pipe(select(state => state.academy));
const user$ = this.store.pipe(select(state => state.user));
return combineLatest(
academy$,
user$
).pipe(
map((data) => {
console.log(data[0]);
console.log(data[1]);
return data;
})
).subscribe((data) => {
console.log(data);
// return true or false to guard
})
}
}
您只是没有订阅您的 observable。
你应该总是 return 下一个链操作符的 Observable,在这种情况下它是守卫。
删除 subscribe((data) => {...})
部分。
如果您必须应用自定义逻辑,请在 map
运算符和 return 中执行任何您想要的 return 即 true
或 false