angular - Angular2.0+ 中的状态管理

标签 angular redux

我们正在开发一个应用程序,其中我们需要一些共享数据,例如不同组件模块之间。

我们无法将其存储在本地或全局存储中,因为它是敏感信息,因此我们不希望用户可以打开开发人员工具并从那里查看数据。

我探索过我们,并且了解到我们可以使用 Redux 或 RXJS。

我正在尝试通过 redux 来实现它,如果我想在商店中存储简单的用户名、国家/地区及其纬度,任何人都可以帮助我需要做什么

最佳答案

是的,您的研究使您处于完美的位置。但我想说你也可以使用其他方法。

关于 Ngrx

首先什么是 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.

Angular 中的 Redux ?

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

在哪里使用它?

A small scenario will make its use quite clear

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.

共享服务

共享服务只不过是 ngrx 的较小实现,并且基于 Observables 。我建议如果您的应用程序大小很大,那么请使用 ngrx else 共享服务。

检查链接以获取更多信息以及如何使用它们。

关于angular - Angular2.0+ 中的状态管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46679868/

相关文章:

Angular 2 - 找不到模块 : Error: Can't resolve '@angular/material'

Angular 6/在 tsconfig.lib.json 中声明库的路径

reactjs - 如何使用 redux-saga 在 getInitialProps 中获取数据。然后在 getInitialProps 方法中从 redux store 获取响应?

reactjs - 如何在Redux Action Creator中通过多个请求获取数据,然后将收集到的数据发送到Reducer?

javascript - Reduxjs 或简单地声明

firebase - async/await 与 firebase .on() 数据库监听器

javascript - Flux 架构比 MVC 有何优势

javascript - Angular 4 中的 HTTP post 请求单元测试

javascript - Angular2 和 InstaFeed 查询

javascript - 如果没有输入任何输入(包括 Angular 中的空格字符),如何禁用按钮