我们正在开发一个应用程序,其中我们需要一些共享数据,例如不同组件模块之间。
我们无法将其存储在本地或全局存储中,因为它是敏感信息,因此我们不希望用户可以打开开发人员工具并从那里查看数据。
我探索过我们,并且了解到我们可以使用 Redux 或 RXJS。
我正在尝试通过 redux 来实现它,如果我想在商店中存储简单的用户名、国家/地区及其纬度,任何人都可以帮助我需要做什么
最佳答案
是的,您的研究使您处于完美的位置。但我想说你也可以使用其他方法。
- Ngrx - Angular 中的 Redux 架构
- Shared Services
关于 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/