简体   繁体   English

解析器与 Redux

[英]Resolver vs Redux

I have been working for quite some time now in small/medium sized projects with Angular and as long as there is some data which needs to be loaded from a server, the team directly goes for storing it into a Redux store.我已经在 Angular 的中小型项目中工作了很长时间,只要有一些数据需要从服务器加载,团队就会直接将其存储到 Redux 存储中。 This allows for the data to be preserved when a user navigates between "pages" and if they decide to refresh their page.这允许在用户在“页面”之间导航以及他们决定刷新页面时保留数据。

However, recently I have been doing a "proper" Angular tutorial and we managed to achieve the same results with a combination of service (provided in in app.module.ts ) to keep the data and resolvers .但是,最近我一直在做一个“正确的” Angular 教程,我们设法通过结合服务(在app.module.ts中提供)来保留数据和解析器来实现相同的结果。 The resolver makes sure that when my main page is loaded, the data required is loaded into the service.解析器确保在加载我的主页时,将所需的数据加载到服务中。 Additionally, if the data is not too big I can even store it into the localStorage and thus, eliminating the HTTP requests in the resolver if the data is not there.此外,如果数据不是太大,我什至可以将其存储到localStorage中,因此,如果数据不存在,则消除解析器中的HTTP请求。

Except for the unlikely use case where 1. there needs to be a lot of data loaded and 2. the user refreshes for some reason the webpage very often, I don't really see why we should be implementing a complete redux store.除了不太可能的用例 1. 需要加载大量数据和 2. 用户出于某种原因经常刷新网页,我真的不明白为什么我们应该实现一个完整的 redux 存储。

Are there more reasons to use Redux which I have not understood or are there more downsides of using this approach?是否有更多理由使用我不理解的 Redux,或者使用这种方法有更多缺点?

This is a very famous article from the creator of redux. 是redux的创造者的一篇非常有名的文章。 It's called you might not need redux.它被称为你可能不需要redux。

Angular is a framework that is known to have solved the problem of inter-data dependency. Angular 是一个已知解决数据间依赖问题的框架。 If the inter-component communication of event and data binding works for you and caching via services is ok without causing the following problems, then you should stick with them.如果事件和数据绑定的组件间通信对您有效并且通过服务进行缓存是可以的,不会导致以下问题,那么您应该坚持使用它们。

Why?为什么?

  1. Redux is counterintuitive for most developers with a steep learning curve Redux 对于大多数学习曲线陡峭的开发人员来说是违反直觉的
  2. Requires a lot of boilerplate code, that if you don't need it's tradeoffs then you shouldn't bother with it需要大量样板代码,如果你不需要它的权衡,那么你不应该打扰它

Redux and flux in general is misinterpreted and widely used in a wrong manner. Redux 和焊剂通常被误解并以错误的方式广泛使用。 There are indications of when you need to use redux which you can see in angular univercity .有迹象表明您需要使用 redux ,您可以在angular univercity中看到。 It pretty much says the following它几乎说以下内容

React components are arranged in a hierarchy. React 组件按层次结构排列。 Most of the time, your data model also follows a hierarchy.大多数情况下,您的数据 model 也遵循层次结构。 In these situations Flux doesn't buy you much.在这些情况下,Flux 不会给您带来太多收益。 Sometimes, however, your data model is not hierarchical.但是,有时您的数据 model 不是分层的。 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.当你的 React 组件开始接收感觉无关紧要的 props,或者你有少量组件开始变得非常复杂时,那么你可能想要研究 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 (ie makes their interface make less sense)你有一条数据需要在你的应用程序的多个地方使用,通过 props 传递它会使你的组件打破单一职责原则(即让它们的界面变得不那么有意义)

There are multiple independent actors (generally, the server and the end-user) that may mutate that data有多个独立的参与者(通常是服务器和最终用户)可能会改变该数据

In any other case (and when the examples above are limited), I would choose to go without redux and use the Resolver, Guards and services that angular serves out of the box.在任何其他情况下(并且当上述示例受到限制时),我会选择不使用 redux 的 go 并使用 angular 开箱即用的解析器、防护和服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM