Angular 连接2个可观察流,但具有不同的访问器
我想,非常像ngrx的选择器定义,在这里你可以插入大量的观测值,并有一个投影函数来返回一些东西 范例Angular 连接2个可观察流,但具有不同的访问器,angular,rxjs,Angular,Rxjs,我想,非常像ngrx的选择器定义,在这里你可以插入大量的观测值,并有一个投影函数来返回一些东西 范例 export const selectCommunityByRouteId = createSelector( selectRouteId, communitySelectors.selectEntities, (id, communities) => { if (id) { return communities.find(c => c.id ===
export const selectCommunityByRouteId = createSelector(
selectRouteId,
communitySelectors.selectEntities,
(id, communities) => {
if (id) {
return communities.find(c => c.id === id);
}
}
);
^有一个selectRouteId
和社区选择器。selectEntities
,两个可观察的流和一个投影函数,该函数接受输入并返回内容
下面的代码在我的组件中。基本上,我所做的是确定一个社区的所有者是否与访问该社区的所有者相同,如果是,则设置要查看的“仅管理员”菜单项
当前
this.community$.pipe(
takeUntil(this.ngDestroyed$),
switchMap(community => this.store.select(selectProfileByID(community && community.owner_id))),
).subscribe(profile => {
if (profile) {
const claims = this.oAuthService.getIdentityClaims();
if (claims) {
// @ts-ignore
const sub = claims.sub;
if (sub === profile.subject_id) {
this.store.dispatch(setDrawerAdminNavigationItems({items: [
{title: 'New Forum', route: ['/new/forum']}
]}));
}
}
} else {
this.store.dispatch(setDrawerAdminNavigationItems({items: undefined}));
}
});
}
然而,现在我在巢穴和社区容器论坛关系中更深了两层,我试图给所有者创建新论坛的可能性。但是,为此,我需要知道当前正在查看的容器的id。我有一个
this.container$ = this.store.select(selectContainerByRouteId);
这不是问题所在。
这个问题是,我需要访问来自这个选择器/可观察对象的信息。
所以,在我的订阅中,我需要的不仅仅是
this.store.select(selectProfileByID(community && community.owner_id)
而且
this.container$
// or
this.store.select(selectContainerByRouteId)
所以我有点像
.subscribe([profile, container] => {
// do something with that
});
或者,更详细地说
期望的:
this.community$.pipe(
takeUntil(this.ngDestroyed$),
switchMap(community => this.store.select(selectProfileByID(community && community.owner_id))),
).subscribe([profile, container] => {
if (profile) {
const CONTAINER_ID_HERE = container.id;
const claims = this.oAuthService.getIdentityClaims();
if (claims) {
// @ts-ignore
const sub = claims.sub;
if (sub === profile.subject_id) {
this.store.dispatch(setDrawerAdminNavigationItems({items: [
{title: 'New Forum', route: ['/new/forum', CONTAINER_ID_HERE]}
]}));
}
}
} else {
this.store.dispatch(setDrawerAdminNavigationItems({items: undefined}));
}
});
}
我知道有forkJoin,但这只是将所有流合并成一个流
我要寻找的是一种从多个流中获取数据并将其传递给下一个函数的方法。我相信你应该这样做
CombineTest的函数应该是什么样的?我还能用开关地图吗?我甚至可以通过管道传递它吗?我自己设法解决了它,但是我收到了一条关于combinelateest
的弃用警告。
我找到了一个,
还有
无论如何,这是我目前正在工作的解决方案
this.community$.pipe(
takeUntil(this.ngDestroyed$),
switchMap(community => {
return combineLatest(this.store.select(selectProfileByID(community && community.owner_id)), this.container$);
}),
).subscribe(([profile, container]) => {
if ((profile) && (container)) {
const claims = this.oAuthService.getIdentityClaims();
if (claims) {
// @ts-ignore
const sub = claims.sub;
if (sub === profile.subject_id) {
this.store.dispatch(setDrawerAdminNavigationItems({
items: [
{title: 'New Forum', route: ['/new/forum', container.id]}
]
}));
}
}
} else {
this.store.dispatch(setDrawerAdminNavigationItems({items: undefined}));
}
});
你在发布的同时回答了自己的问题,这让人印象深刻。是的,我不想把它扔掉,也许有人会从中受益。这种情况经常发生,你把你的问题写下来,然后你就有了一堆文字,你就解决了它。但是当你删除你写的东西时,所有的时间和精力都被浪费了。幸运的是,当您提交表单时,可以选择回答您自己的问题。combinelateest
仍然受支持,但不支持多个参数,现在combinelateest
将单个数组作为参数。只需将参数包装在数组中,您就可以摆脱弃用警告。谢谢。执行此操作的提交: