简体   繁体   English

angular 2 应用的状态管理

[英]State management of angular 2 application

I am new in Angular2.我是 Angular2 的新手。 Angular provides various options for managing the state of an application like ngrx/store, angular services etc. if we manage the state of the application using ngrx/store then we require an external library but in case of angular services do not require. Angular 提供了各种管理应用程序状态的选项,如 ngrx/store、angular services 等。如果我们使用 ngrx/store 管理应用程序的状态,那么我们需要一个外部库,但在 angular services 的情况下不需要。 What should be used for managing the state of an application?应该使用什么来管理应用程序的状态? what are the pros and cons of each approach?每种方法的优缺点是什么? Is there any recommendation that is provided by the angular community? angular 社区是否提供了任何建议?

You do not need ngrx/store to implement a redux-store.您不需要 ngrx/store 来实现 redux-store。 You can implement one yourself.你可以自己实现一个。 There are multiple tutorials online showing how to do it in a lightweight way.网上有多个教程展示了如何以轻量级的方式进行操作。 But ngrx/store is developed and supported by people from google and so it is a mature framework you can trust, which also provides some neat features, like effects for asynchronous events and immutability checks in development.但是 ngrx/store 是由 google 的人开发和支持的,因此它是一个您可以信赖的成熟框架,它还提供了一些简洁的功能,例如异步事件的效果和开发中的不变性检查。

The main question is whether or not you should use the redux pattern/store in general.主要问题是您是否应该使用 redux 模式/存储。 This highly depends on your application architecture.这在很大程度上取决于您的应用程序架构。 For smaller applications it could be too much work, especially if you don't have clear user stories and things might change quickly.But if you have a big SPA, in general it is recommended to use a central data flow store, eg redux.对于较小的应用程序,它可能工作量太大,特别是如果您没有明确的用户故事并且事情可能会很快发生变化。但是如果您有一个大型 SPA,通常建议使用中央数据流存储,例如 redux。 It ensures the dataflow in your application is unidirectional and there are no circular dependencies at important parts of your application, causing weird bugs in the future as your application gets more complex.它确保您的应用程序中的数据流是单向的,并且在您的应用程序的重要部分没有循环依赖,随着您的应用程序变得越来越复杂,将来会导致奇怪的错误。

Also even in a SPA you have things like UI-Libraries which do not belong in a store.此外,即使在 SPA 中,您也有不属于商店的 UI-Libraries 之类的东西。 A button prototype being enabled or disabled is something only its component should be aware of.按钮原型被启用或禁用是只有它的组件应该知道的事情。 But the same button in usage in the application module should represent a particular functionality being offered or not and that should be represented in redux.但是应用程序模块中使用的相同按钮应该代表提供或不提供的特定功能,并且应该在 redux 中表示。

I highly suggest you look at the ngrx-store example app and see the benefits and shortcomings yourself.我强烈建议您查看 ngrx-store 示例应用程序,并亲自了解其优点和缺点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM