Before I start, a small backstory. So, a few weeks back, there was a requirement in an angular project I was working on, wherein I had to keep a piece of data intact so that several other components could use it. My React instinct suggested me of using some angular version of context API which led me to 3 Angular ways of managing states and keeping data intact.
Component State: For Simple applications or components with minimal shared data, we can manage state within individual components using Angular's component properties and event binding. This was clearly out of league because I needed to share data across multiple components.
Service Based State Management: Services can be used to create centralized state management solutions. But performance and scalability of the application will take a hit following this approach.
ngRx Store: ngRx store provides centralized state management capabilities. NGRX uses RxJS observables and actions to manage state.
Out of the three ngRx Store seemed to be the most suitable one to go with.
Implementing ngRx Store:
To better understand the implementation of ngRx, we first need to understand how ngRx works in the first place.
The flow starts from the component. The good thing about ngRx is our component doesn't need to know how to manage the state. Only thing it is concerned about is dispatching an action to inform something happened (an event) like the user clicked on the delete button.
At this point the reducer comes into play. Reducer is responsible for determining how a particular action should modify state. Usually we have reducer for every entity we want to manage state for. So, Reducer detect all the action being dispatched in the app and determine how the state should be modified and once modified, stores the updated state in the ngRx Store (which is a global store for all the states across the application).
Finally, when a component wants to access the store, the component uses a selector to pull in the state that it needs from the store.
Now let's get our (actually my) hands dirty and implement a store for a Todo application.
Step 1: Creating action
Inside the createAction method, we are passing two parameters - a unique string that will be used to distinguish an action, another is the payload we want to send (optional).
Step 2: Creating Reducers
In the reducer, we pass the initial state and the task to perform on dispatching a particular action.
Step 3: Dispatching Action
We dispatch an action by calling the dispatch method of the store passing the action to it.
Step 4: Creating selector
AppState refers to the central store for the whole application from which we selecting the todo state. Now we can start consuming the store data like I did in the Dispatching Actions code's line no. 11.
Finally, we need to add the todoReducer in our imports of the App Module so that it is available across the application.
Conclusion
In this blog, I summarized and explained the ngRx store in details, how it works and how can we implement it in our application. ngRx store can be very useful as the application grows, to store data and access it across different components. Hope it helped ;)
以上是ngRx Store in Angular的详细内容。更多信息请关注PHP中文网其他相关文章!

JavaScript字符串替换方法详解及常见问题解答 本文将探讨两种在JavaScript中替换字符串字符的方法:在JavaScript代码内部替换和在网页HTML内部替换。 在JavaScript代码内部替换字符串 最直接的方法是使用replace()方法: str = str.replace("find","replace"); 该方法仅替换第一个匹配项。要替换所有匹配项,需使用正则表达式并添加全局标志g: str = str.replace(/fi

因此,在这里,您准备好了解所有称为Ajax的东西。但是,到底是什么? AJAX一词是指用于创建动态,交互式Web内容的一系列宽松的技术。 Ajax一词,最初由Jesse J创造

10款趣味横生的jQuery游戏插件,让您的网站更具吸引力,提升用户粘性!虽然Flash仍然是开发休闲网页游戏的最佳软件,但jQuery也能创造出令人惊喜的效果,虽然无法与纯动作Flash游戏媲美,但在某些情况下,您也能在浏览器中获得意想不到的乐趣。 jQuery井字棋游戏 游戏编程的“Hello world”,现在有了jQuery版本。 源码 jQuery疯狂填词游戏 这是一个填空游戏,由于不知道单词的上下文,可能会产生一些古怪的结果。 源码 jQuery扫雷游戏

本教程演示了如何使用jQuery创建迷人的视差背景效果。 我们将构建一个带有分层图像的标题横幅,从而创造出令人惊叹的视觉深度。 更新的插件可与JQuery 1.6.4及更高版本一起使用。 下载

本文讨论了在浏览器中优化JavaScript性能的策略,重点是减少执行时间并最大程度地减少对页面负载速度的影响。

Matter.js是一个用JavaScript编写的2D刚体物理引擎。此库可以帮助您轻松地在浏览器中模拟2D物理。它提供了许多功能,例如创建刚体并为其分配质量、面积或密度等物理属性的能力。您还可以模拟不同类型的碰撞和力,例如重力摩擦力。 Matter.js支持所有主流浏览器。此外,它也适用于移动设备,因为它可以检测触摸并具有响应能力。所有这些功能都使其值得您投入时间学习如何使用该引擎,因为这样您就可以轻松创建基于物理的2D游戏或模拟。在本教程中,我将介绍此库的基础知识,包括其安装和用法,并提供一

本文演示了如何使用jQuery和ajax自动每5秒自动刷新DIV的内容。 该示例从RSS提要中获取并显示了最新的博客文章以及最后的刷新时间戳。 加载图像是选择


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver CS6
视觉化网页开发工具

WebStorm Mac版
好用的JavaScript开发工具