问题
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