Angular2 NgRx。存储和提取选择器以供重用
这是我的店铺:Angular2 NgRx。存储和提取选择器以供重用,angular,selector,projection,ngrx,Angular,Selector,Projection,Ngrx,这是我的店铺: { comments:{ entities:{id:comment} ids:[ids...] }, videos:{ entities:{id:video} ids:[ids...] } } export function getVideoById(id) { return (state$: Observable<AppState>) => _getVideoById(s, id); } function
{
comments:{
entities:{id:comment}
ids:[ids...]
},
videos:{
entities:{id:video}
ids:[ids...]
}
}
export function getVideoById(id) {
return (state$: Observable<AppState>) => _getVideoById(s, id);
}
function _getVideoById(s: AppState, id: string) {
return s.videos.entities[id];
}
export function getCommentsByVideoId(id) {
return (state$: Observable<AppState>) =>
state$.map((s: AppState) => _getCommentsByVideoId(s, id))
}
function _getCommentsByVideoId(s: AppState, id) {
let currentVid = _getVideoById(s, id);
return currentVid.commentIds.map(id => s.comments.entities[id]);
}
this.video$ = this.store.let(getVideoById(this.videoId));
this.comments$ = this.store.let(getCommentsByVideoId(this.videoId));
以下是我的预测:
{
comments:{
entities:{id:comment}
ids:[ids...]
},
videos:{
entities:{id:video}
ids:[ids...]
}
}
export function getVideoById(id) {
return (state$: Observable<AppState>) => _getVideoById(s, id);
}
function _getVideoById(s: AppState, id: string) {
return s.videos.entities[id];
}
export function getCommentsByVideoId(id) {
return (state$: Observable<AppState>) =>
state$.map((s: AppState) => _getCommentsByVideoId(s, id))
}
function _getCommentsByVideoId(s: AppState, id) {
let currentVid = _getVideoById(s, id);
return currentVid.commentIds.map(id => s.comments.entities[id]);
}
this.video$ = this.store.let(getVideoById(this.videoId));
this.comments$ = this.store.let(getCommentsByVideoId(this.videoId));
我的问题:
我目前正在将我的投影分成两部分,以便可以重用其他投影中的一些部分(即:let currentVid=\u getVideoById(s,id);)
是否有一种方法可以在一个块中声明我的投影,并在其他投影中重用它们
一种投影合成
我见过这样的例子,但每次它们“缩小”投影的范围(即:我们从视频开始,深入视频,我们再也不能像“评论”那样回到其他分支)。我对这一切都不熟悉,所以如果您能给我一些建议,我将不胜感激
多谢各位 我想出了一种装饰服务(仍然不确定这是不是“正确的方式”) 例如: 希望你能得到我的方法:)你也可以利用一些记忆功能来缓存观测值(它们仍然会发出新的值)
Br您是否查看了
ngrx/example app
中的以获取指导?是的,我不久前确实查看了它(谢谢),我发现它非常复杂,因此我提出了上面的解决方案。。。