Why use NGRX instead of constructor injected services?

爷,独闯天下 提交于 2020-01-13 10:08:13

问题


Wondering why one would use NGRX or NGXS for an Angular app instead of constructor injected services to handle component IO?

Is it only to ensure that component properties references are never mutated without switching out the entire property value reference or is there more to it?

Altnernative to NGRX

per the answer I developed:

Slice.

I believe it does everything NgRx / NgXS does (With the exception of a time machine - but this is easy to implement via delta notifications - already supported). but with zero boilerplate.

Here's an article showcasing some of the capabilities: https://medium.com/@ole.ersoy/storing-users-in-the-reactive-slice-object-store-5ea0fab06256


回答1:


You will need to write a service that provides a consistent and easy to change api to modify the data, you will need to come up with a fast and well tested way of querying the data, you will need to write and maintain observables for all your data. You will have to write and establish a pattern for async calls. You will have to write an api to access the data in your templates.

By the time you are done you will end up with something resembling ngrx.

For a simple one service data, ngrx is overkill, lots of boilerplate, but for a reactive app with multiple data sources or complex data interactions across numerous developers having a well used library is really helpful.

Follow SO answer for more in depth explanation: What are benefits of using store (ngrx) in angular 2

To understand, when to use which approach, read this: RxJs and Ngrx Store - When to Use a Store And Why?



来源:https://stackoverflow.com/questions/49885341/why-use-ngrx-instead-of-constructor-injected-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!