我是NgRx的新手。经过一堆阅读后,我将路由器(if(auth.getcurrentUser() != null){
String email = auth.getcurrentUser().getEmail();
tt1.setText(email);
}
)添加到了应用程序状态以访问选择器中的router参数。我正在将@ngrx/router-store
和CustomRouterSerializer
一起使用。从那时起,我遇到了一个问题,例如,当我从详细信息页面( / hero /:heroId )导航到列表页面( / )时,将会调用详细信息页面中的RouterState.Minimal
选择器,这会导致其他问题。当然,我可以将空支票放入...
但是,由于我是初学者,所以我认为最好问一下。在过渡到另一页时调用getSelectedHero
似乎是错误的。确定路由器状态正在改变,但是?
我花了一些时间试图找到不包含未定义检查的解决方案。
我创建了一个演示该问题的“超级”应用程序。我将其推至GitHub和StackBlitz。
这是详细信息页面( / hero /:heroId )中的选择器代码,当离开该页面时会被调用。
getSelectedHero
在详细信息页面中(/ hero /:heroId)我使用以下内容:
export const getSelectedHero = createSelector(
selectHeroesState, getRouteState, (state,router) => {
const hero = state.entities[router.state.params.heroId];
// FixMe: simplifies the problem... hero is undefined if you navigation for /hero/:heroId to / and following line will throw an error.
console.log("getSelectedHero",hero,hero.id,hero.name);
return hero;
}
);