Angular NgrxStore dispatch Action inside Selector Called many times

问题描述 投票:0回答:0

我正在使用 ngRx store 开发 Angular 项目(我第一次使用它),当我从 Store 获取我的用户时,我想发送 Action 来获取用户课程,但是这个动作被调用了很多次。这是我的代码:

// method called in the NgOnInit
loadUserCourses() {

    this.store.select(getCurrentUserSelector).subscribe((user: UserDTO) => {
      if (user) {
        console.log('dispatch(SearchCoursesByUserIdAction');
        this.store.dispatch(SearchCoursesByUserIdAction({ userId: user.id }));
      }
    });
}

当我检查我的控制台时,我发现控制台消息甚至在网络选项卡中打印了 5 或 6 次,我发现有多个 http 调用。 你知道我的代码中有什么问题吗?派遣我的最佳做法是什么

SearchCoursesByUserIdAction

angular ngrx ngrx-store
© www.soinside.com 2019 - 2024. All rights reserved.