我有一个canActivate,其中我有2个状态
canActivate(): Observable<boolean> {
this.store.dispatch(new fromProjectActions.SelectProjectAction(43))
combineLatest(
this.store.pipe(
select(fromProjectSelectors.getSelectedProject)
),
this.store.pipe(select(fromUserSelectors.getUser))
).pipe(
map(([project, user]) => {
// Logic here
// return true if project.creatorId === user.id
})
);
}
知道getUser
在该状态下已经可用时,我该怎么做,但是getSelectedProject
有必要在此之前分派其动作
编辑:
canActivate(): Observable<boolean> {
this.store.dispatch(new fromProjectActions.SelectProjectAction(43));
combineLatest(
this.store.pipe(
select(fromProjectSelectors.getSelectedProject)
),
this.store.pipe(select(fromUserSelectors.getUser))
).subscribe(([project, user]) => {
// here the first time `project` is `null`, have to wait the
second time
if (project) {
if (project.id === user.id) {
return of(true);
} else {
return of(false);
}
}
});
}
答案 0 :(得分:1)
如果您的问题是select(fromProjectSelectors.getSelectedProject)
发出null
,则可以将其过滤掉:
combineLatest(
this.store.pipe(
select(fromProjectSelectors.getSelectedProject).pipe(
filter(project => !!project))
),
this.store.pipe(select(fromUserSelectors.getUser))
).subscribe( ...