无法使用@ ngrx / effects @ 4.1.1和@ ngrx / store @ 4.1.1从角度4的可观察量中获取计数

时间:2017-12-13 04:17:05

标签: angular ngrx

我试图通过页面的效果将元素插入数据库。单击提交后,页面将重定向到仪表板页面,其中i显示为用户添加到数据库的那些元素的计数。 但重定向成功后(意味着一旦元素成功创建)我无法看到计数,相反,我只能在刷新后看到。 请关注屏幕

This is the creation page

After clicking on the submit button I should be seeing the count as 4 but i see nothing

After refreshing the page i can see the count

在创建页面中,在单击createbutton之后调用以下代码

    this._store.dispatch(new SomeAction.AddNewAction( this.somemodel ));
    this._router.navigate(['/dashboard']);
仪表板构造函数中的

    this._store.dispatch(new SomeAction.LoadModelAction(userid));

    this.modelObserver$ = this._store.select('models');

在dashboard.component.html页面

   <div class="text-right">
       <span class="badge badge-secondary">You have {{ (modelObserver$ | async).length }} model</span>
   </div>

非常感谢任何帮助..

1 个答案:

答案 0 :(得分:0)

我想LoadModelAction正在从API /数据库加载记录。我不确定在构造函数中调度操作是不是一个好主意。

首先:即使你想这样做,你也应该用ngOnInit方法发送它。

其次:您不确定如果您调用此操作,则记录已存在。

如果您正在使用ngrx-effects,解决方案很简单:您应该在成功执行AddNewAction Action后调用LoadModelAction。这意味着您应该至少执行3项操作:AddNewActionAddNewSuccessActionLoadModelAction。最佳解决方案是,如果API将您的模型作为添加新元素的响应返回(在这种情况下,您只需要AddNewActionAddNewSuccessAction)。如果您在理解它时遇到一些问题:分享您的特效代码,我将尝试逐步解释:)