为什么使用NGRX而不是构造函数注入服务? [英] Why use NGRX instead of constructor injected services?

查看:138
本文介绍了为什么使用NGRX而不是构造函数注入服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道为什么会使用 NGRX NGXS 用于Angular应用程序而不是构造函数注入服务来处理组件IO?

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?

根据我开发的答案:

切片

我相信它可以完成NgRx / NgXS所做的一切(除了时间机器 - 但这很容易通过增量通知实现 - 已经支持)。但是没有样板文件。

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.

这篇文章展示了一些功能:
https://medium.com/@ole.ersoy/storing-users-in-the-reactive-slice -object-store-5ea0fab06256

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

推荐答案

您需要编写一个提供一致且易于更改的服务api修改数据,你需要提出一种快速且经过良好测试的查询数据的方法,你需要为所有数据编写和维护observable。您必须编写并建立异步调用的模式。你必须编写一个api才能访问你模板中的数据。

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.

当你完成时,你会得到类似于ngrx的东西。

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

对于简单的一个服务数据,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.

关注SO答案以获得更深入的解释:在角度2中使用商店(ngrx)有什么好处

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

要了解,何时使用哪个方法,请阅读: RxJs和Ngrx商店 - 何时使用商店和为什么?

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

这篇关于为什么使用NGRX而不是构造函数注入服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆