简体   繁体   English

将react-redux集成到现有的Angularjs应用程序中

[英]Integrating react-redux into an existing Angularjs application

I'm considering integrating react-redux into a 3 years old codebase of Angularjs. 我正在考虑将react-redux集成到Angularjs已有3年历史的代码库中。 I don't wish to refactor the all codebase at once, rather incrementally introducing react-redux through new features which my team will develop. 我不想立即重构所有代码库,而是通过我的团队将开发的新功能逐步引入react-redux。

  1. Is there any best practices doing so? 是否有任何最佳做法? (any resources will be appreciated) (任何资源将不胜感激)
  2. Can an old feature (written in ng) contain react-redux parts or I'll have to rewrite it? 旧功能(用ng编写)可以包含react-redux部件,还是我必须重写它?
  3. How can I avoid doing the same API calls? 如何避免进行相同的API调用? (if both need the same resource) (如果两者都需要相同的资源)
  4. Should I avoid the change all together? 我应该一起避免更改吗?
  1. I don't know of any specific best practice, but I recently started using ng-redux ( https://github.com/angular-redux/ng-redux ) in my current Angular 1.5 app to great effect so far. 我不知道任何特定的最佳实践,但是最近我开始在当前的Angular 1.5应用程序中使用ng-redux( https://github.com/angular-redux/ng-redux ),到目前为止效果很好。 It's been really nice for gaining the benefits of uni-directional data flow and structured state management from Redux while still maintaining the Angular core. 从Redux获得单向数据流和结构化状态管理的好处,同时仍保持Angular核心,这真是太好了。 This will also let you migrate over gradually, one entity model at a time. 这也将使您一次一次迁移一个实体模型。 Although I haven't used it myself, I see that https://github.com/ngReact/ngReact is available for wrapping React components in Angular directives. 尽管我自己还没有使用过它,但我看到https://github.com/ngReact/ngReact可用于将React组件包装在Angular指令中。 If you're looking to eventually get to fully using react-redux standalone, that could be a good stepping stone. 如果您希望最终完全使用独立的react-redux,那可能是一个很好的垫脚石。
  2. There was some refactoring required to integrate ng-redux into existing features, but if you're already maintaining models for your data outside controllers, it isn't too bad. 要将ng-redux集成到现有功能中需要进行一些重构,但是如果您已经在控制器外部维护了数据模型,那还不错。 Ng-redux lets you call Action Creator functions directly from your controller, which will typically replace UI action event handlers. Ng-redux允许您直接从控制器调用Action Creator函数,该控制器通常将替换UI动作事件处理程序。
  3. You can easily make your API calls using Async Actions with redux-thunk when using ng-redux. 使用ng-redux时,您可以使用redux-thunk与Async Actions轻松进行API调用。 You can just use existing Angular services to request out to your API, and then use the response to dispatch a standard Action to update state. 您可以只使用现有的Angular服务向您的API发出请求,然后使用响应来调度标准的Action来更新状态。
  4. I'd say it's definitely worth it to at least integrate ng-redux for standardizing the data flow of your app. 我想说,至少集成ng-redux来标准化应用程序的数据流绝对是值得的。 It will help organize your project better, and might improve performance depending on how you refactor. 这将有助于更好地组织项目,并可能根据您的重构方式提高性能。 The benefit of ngReact is less clear to me though, unless you are looking to slowly convert components until you can ditch the Angular core entirely. ngReact的好处对我来说还不太清楚,除非您希望慢慢地转换组件,直到可以完全放弃Angular核心为止。

I've successfully migrated the application under the brown-field limitations I pointed out at the original question. 我已经成功地迁移下的棕地限制我在原来的问题指出了应用。

The solution is based on two approaches: 该解决方案基于两种方法:

  1. Small Components - are components that are pretty much contained. 小型组件-几乎包含在内的组件。 For example, notifications dropdown. 例如,通知下拉列表。
  2. Entire part of the App that I can't migrate yet - basically some routes/views that are still written in Angular. 我还无法迁移的整个应用程序部分-基本上仍然是一些用Angular编写的路线/视图。

Both approached use a similar technique to the one which suggested here: http://softeng.oicr.on.ca/chang_wang/2017/04/17/Using-AngularJS-components-directives-in-React/?utm_source=reactnl&utm_medium=email 两种方法都使用与此处建议的方法类似的技术: http : //softeng.oicr.on.ca/chang_wang/2017/04/17/Using-AngularJS-components-directives-in-React/?utm_source=reactnl&utm_medium=电子邮件

TIP: It's much more easier if your Angular dependencies are managed properly. 提示:如果对Angular依赖项进行了适当的管理,它将更加容易。

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

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