angular - 解析器 vs Redux

标签 angular redux angular-resolver angular-redux

我已经在 Angular 的中小型项目中工作了一段时间,只要有一些数据需要从服务器加载,团队就会直接将其存储到 Redux 存储中。当用户在“页面”之间导航以及决定刷新页面时,这允许保留数据。

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

除了不太可能的用例,即 1. 需要加载大量数据以及 2. 用户由于某种原因经常刷新网页,我真的不明白为什么我们应该实现一个完整的 redux 存储.

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

最佳答案

This这是 redux 创建者的一篇非常著名的文章。这就是所谓的“你可能不需要 redux”。

Angular 是一个众所周知解决了数据间依赖问题的框架。如果事件和数据绑定(bind)的组件间通信适合您,并且通过服务进行缓存也可以,不会导致以下问题,那么您应该坚持使用它们。

为什么?

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

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

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 提供的开箱即用的解析器、守卫和服务。

关于angular - 解析器 vs Redux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58853513/

相关文章:

html - 将文件输入 FileReader base64 结果动态分配给 IMG SRC

javascript - React函数参数是Proxy?

javascript - 使用 redux-thunk/redux-observable 和 redux 的回调

javascript - 使用中间件检查用户 session react redux

Angular 4 - 对应用程序组件使用解析器

javascript - 通过发出 http 请求来获取路由解析器的数据,以便第二个请求的结果取决于第一个请求的结果

angular - 在 html datepicker 上动态设置 minDate/maxDate

html - 如何使一个 div 最初隐藏并在输入时加载?

ios - null 不是对象(评估 '__gCrWeb.form.wasEditedByUser.set' )

返回 EMPTY 时,Angular 8 路由解析器不会取消路由导航