解析器与 Redux

Resolver vs Redux

我现在已经在 small/medium 规模的项目中工作了一段时间 Angular 只要有一些数据需要从服务器加载,团队就会直接进行将其存储到 Redux 存储中。这允许在用户在 "pages" 之间导航以及他们决定刷新页面时保留数据。

不过,最近我一直在做一个 "proper" Angular 教程,我们设法通过组合服务(在 app.module.ts 中提供)取得了相同的结果,以保持数据和 resolvers。解析器确保当我的主页被加载时,所需的数据被加载到服务中。此外,如果数据不是太大,我什至可以将其存储到 localStorage 中,因此,如果数据不存在,则消除解析器中的 HTTP 请求。

除了不太可能的用例 1. 需要加载大量数据和 2. 用户出于某种原因经常刷新网页,我真的不明白为什么我们应该实施一个完整的redux 商店。

是否还有更多我不理解的使用 Redux 的理由,或者使用这种方法是否有更多缺点?

This 是 redux 作者的一篇非常有名的文章。它叫做你可能不需要 redux。

Angular是众所周知的解决数据间依赖问题的框架。如果事件和数据绑定的组件间通信适合您,并且通过服务进行缓存没有问题,那么您应该坚持使用它们。

为什么?

  1. 对于大多数学习曲线陡峭的开发人员来说,Redux 是违反直觉的
  2. 需要大量样板代码,如果您不需要它的权衡,那么您不应该为它烦恼

Redux 和 Flux 通常被误解并以错误的方式广泛使用。在 angular univercity 中可以看到何时需要使用 redux 的指示。它几乎说了以下内容

React components are arranged in a hierarchy. Most of the time, your data model also follows a hierarchy. In these situations Flux doesn’t buy you much. Sometimes, however, your data model is not hierarchical. When your React components start to receive props that feel extraneous, or you have a small number of components starting to get very complex, then you might want to look into Flux.

You have a piece of data that needs to be used in multiple places in your app, and passing it via props makes your components break the single-responsibility principle (i.e. makes their interface make less sense)

There are multiple independent actors (generally, the server and the end-user) that may mutate that data

在任何其他情况下(当上面的示例有限时),我会选择不使用 redux 并使用 angular 开箱即用的解析器、守卫和服务。