在 ngrx 中使用带有 props 的选择器的替代方法是什么

wal*_*exy 9 ngrx

我正在ngrx 网站selectors上阅读有关内容,我偶然发现了.selectorsprops

ngrx 团队表示它已被弃用,但他们没有提供替代方案。我想在我的应用程序中使用selectorswith props

除了使用带有 props 的选择器之外,还有其他选择吗?

如果有的话我该怎么办?

D M*_*D M 25

通知中的“已弃用”一词实际上是一个链接,尽管由于其所在横幅的颜色而很难注意到。它链接到讨论带有 props 的选择器的 GitHub Issue

这个问题引起了人们的担忧,由于 Angular 12+ 默认处于严格模式,开发人员在使用 props 实现选择器时可能会遇到一个缺点 - 即 prop 值不是强类型的,或者可能是错误类型的。

建议的方法是使用工厂选择器。本质上,您不是将 props 作为选择器的参数传递,而是将 props 作为参数传递给选择器定义的函数。

所以而不是:

export const getCount = createSelector(
  getCounterValue,
  (counter, props) => counter * props.multiply
);

const count = this.store.select(getCount, 5);
Run Code Online (Sandbox Code Playgroud)

你会使用:

export const getCount = (multiply: number) => createSelector(
  getCounterValue,
  (counter) => counter * multiply
);

const count = this.store.select(getCount(5));
Run Code Online (Sandbox Code Playgroud)

Gunnar B.的评论有助于链接到迁移指南,其中包含有关从带有 props 的选择器迁移到工厂选择器的部分(如果您需要更多信息)。