RxJS newb在这里。使用Angular 6.试图弄清楚如何从Observable<T>
获取Observable<Observable<T>>
。不确定这是否有效,并且从概念上难以理解,但这似乎是一个简单的问题。
我研究过switchMap,flatMap,forJoin,但是我认为它们不适合我的需求。
我想做的是一个Angular路由防护,它将阻止用户访问路由,除非他们具有必要的权限。 2个依赖项是用于从中获取其信息的用户配置文件,然后用于获取其权限。这种混合导致了“可观察的”问题。这就是我所拥有的:
export class AuthPermissionsRouteGuard implements CanActivate {
constructor(
private router: Router,
private authService: AuthPermissionsService,
private openIdService: AuthOpenIdService) {}
/**Navigates to route if user has necessary permission, navigates to '/forbidden' otherwise */
canActivate(routeSnapshot: ActivatedRouteSnapshot): Observable<boolean> {
return this.canNavigateToRoute(routeSnapshot.data['permissionId'] as number);
}
/**Waits on a valid user profile, once we get one - checks permissions */
private canNavigateToRoute(permissionId: number): Observable<boolean> {
const observableOfObservable = this.openIdService.$userProfile
.pipe(
filter(userProfile => userProfile ? true : false),
map(_ => this.hasPermissionObservable(permissionId)));
// Type Observable<Observable<T>> is not assignable to Observable<T> :(
return observableOfObservable;
}
/**Checks if user has permission to access desired route and returns the result. Navigates to '/forbidden' if no permissions */
private hasPermissionObservable(permissionId: number): Observable<boolean> {
return this.permissionsService.hasPermission(permissionId).pipe(
map(hasPermission => {
if (!hasPermission) {
this.router.navigate(['/forbidden']);
}
return hasPermission;
}
));
}
}
非常感谢!
答案 0 :(得分:1)
就目前而言,您正在从hasPermissionObservable
函数返回一个Observable,它将被包装在map operator
中的Observable中。
您需要查看mergeMap/flatMap operator
或contactMap operator
。
MergeMap:当您希望展平内部可观察对象但希望手动控制内部订阅的数量时,最好使用此运算符。学习RXJS链接中的示例:
// RxJS v6+
import { of } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
// emit 'Hello'
const source = of('Hello');
// map to inner observable and flatten
const example = source.pipe(mergeMap(val => of(`${val} World!`)));
// output: 'Hello World!'
const subscribe = example.subscribe(val => console.log(val));
ContactMap:将值映射到内部可观察的,按顺序订阅和发出。学习RXJS链接中的示例:
// RxJS v6+
import { of } from 'rxjs';
import { concatMap } from 'rxjs/operators';
// emit 'Hello' and 'Goodbye'
const source = of('Hello', 'Goodbye');
// example with promise
const examplePromise = val => new Promise(resolve => resolve(`${val} World!`));
// map value from source into inner observable, when complete emit result and move to next
const example = source.pipe(concatMap(val => examplePromise(val)));
// output: 'Example w/ Promise: 'Hello World', Example w/ Promise: 'Goodbye World'
const subscribe = example.subscribe(val =>
console.log('Example w/ Promise:', val)
);
因此,您的示例:
/**Waits on a valid user profile, once we get one - checks permissions */
private canNavigateToRoute(permissionId: number): Observable<boolean> {
const observableOfObservable = this.openIdService.$userProfile
.pipe(
filter(userProfile => userProfile ? true : false),
concatMap(_ => this.hasPermissionObservable(permissionId))); // <- try changes here
// Type Observable<Observable<T>> is not assignable to Observable<T> :(
return observableOfObservable;
}