如何实现 Redux?

How to implement Redux?

即使要处理的数据不在不同组件之间共享,我是否应该对所有 API 调用使用 Redux? 示例:用户列表组件必须检索用户列表并将其显示在界面中。这些用户不在此组件之外使用。 API 调用是否必须用 actions/reducers/effects 实现?

如果数据和逻辑都是自包含的,那么就没有必要使用 redux 模式,因为它只会引入不必要的复杂性。

https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367

当然这取决于你的目标。如果您只想尝试 Angular,这不是您的选择。如果您计划您的应用程序会增长 - 这不是一个坏主意。实际上,如果您将使用 "module by feature" 结构,您以后可以轻松地将 ngrx 添加到您的项目中。

Redux 是关于状态管理的。也许你现在认为这是矫枉过正。但请确保您的应用程序 grow.You 将获得更多模块,处理所有内容将更加困难。

此外,redux 为您的应用程序添加了一些结构。我相信如果有新人加入团队,如果团队使用 redux,新人会很容易地继续项目。即使他们是初级用户,让他们阅读 redux/ngrx,他们也可以轻松跟踪数据在您的应用程序中的流动方式。实际上,即使 React dev 来到 angular 团队也是如此,反之亦然。至少对我来说:我和我的同事可以轻松地讨论问题并成功解决它们,但我使用 Angular 而他使用 React。

您是否应该按照 redux 流程进行 API 调用?出色地。从技术上讲,您可以忽略这一点。但为什么?围绕 Redux 有很多有用的工具。例如 redux 开发工具。如果您使用操作进行 api 调用,那么您也会在 Redux 开发工具中看到它们。您可以看到操作列表、操作负载(您从服务器收到的内容)以及此操作如何改变您的状态。

首先什么是Redux?

Redux is an application state manager for JavaScript applications, and keeps with the core principles of the Flux-architecture by having a unidirectional data flow in your application. Redux applications have only one global, read-only application state. This state is calculated by "reducing" over a collection or stream of actions that update it in controlled ways.

Redux 在 Angular ?

Redux state managers have been very well received and have inspired the creation of @ngrx, a set of modules that implement the same way of managing state as well as some of the middleware and tools in the Redux ecosystem. @ngrx was created to be used specifically with Angular and RxJS, as it leans heavily on the observable paradigm. Both the Components you see above are managed using ngrx store (updated to v4) Advantages of upgrading now we have support for lazy loading directly in ngrx using Featured Module

在哪里使用它?

一个小场景,使用起来就很清楚了

I am hoping you might have used facebook right :P so if you look @ facebook's website you will see multiple components like navbar, left panel, chatbox etc. Ever wondered when a new message is delivered to you all these are updated simultaneously how ? If they went for srevices or Event Emitters they would be engulfed in a sea of spaghetti code. Which even if implemented will be very hard to debug or make changes .

Here comes the use of ngrx state management all the information is stored @ a single place and all the components are identified when the state of the data is changed . A Look this answer from stack overflow will clear the concepts even more Image clear.

如何实现Redux?

Angular有两种选择,一种是ng-redux,另一种是ngrx。我个人喜欢 ngrx,因为它有庞大的社区和很棒的想法。

您可以查看@this link 的计数器应用程序并将列表添加到 Ngrx v4。

如果您想看一下@使用整个 ngrx 套件实现的整个应用程序,请看一下 this git repo this is deployed at link

对此的引用来自我的 gh-page,我在那里传播了 Angular 概念。