5

Similar to the ngrx/example-app, I am building an interface where users will be able to search for something by entering a text string.

I would like to update the url with an optional param (query=search_string) so that when the user returns to their search with the back button they return to the same search already loaded. (I'll also be doing the same thing for the page they are on)

What would be the appropriate way to add optional params with ngrx?

My thought is to put this into BookEffects since we do not want to update the route prior to the debounce time. But for some reason that doesn't feel right...

[Edit] To expand on this further, when the user navigates back to the search page, the search would need to be bootstrapped somehow. So my thought would be to also modify the FindBookPageComponent to include:

constructor(..., private route: ActivatedRoute) {
  ...
}

ngOnInit() {
  let query = this.route.snapshot.params['query'];
  if (query) {
    this.store.dispatch(new book.SearchAction(query));
  }
}

This still feels awkward because there would be an unnatural delay before the search results appear due to the debounce in the SearchAction. So does this mean we should also create a separate search action: ImmediateSearchAction?

4

1 回答 1

0

ngrx 中没有任何内容说明您应该如何实现它,这取决于您。

您可以制作 ImmediateSearchAction 并在 SearchAction 效果中使用 debounce 将其转换为立即搜索。通过这样做,您可以轻松地重用现有的 reducer,并且仍然保持操作按上下文分开。

我会做什么:

  • 创建新的 UserInputSearchAction,(这是去抖动的)
  • 创建新的 UrlSearchAction,通过效果映射到 SearchAction
  • 立即执行 SearchAction

推荐阅读/视频:

于 2018-07-02T11:46:54.700 回答