简体   繁体   English

如何将 angularjs 服务迁移到 redux?

[英]How to migrate an angularjs service to redux?

I am migrating a AngularJS 1.4 app to React.我正在将 AngularJS 1.4 应用程序迁移到 React。 I migrated few directives to React components using react2angular .我使用react2angular将一些指令迁移到 React 组件。 Now I am in a stage to migrate services to Redux.现在我正处于将服务迁移到 Redux 的阶段。 I don't want to use ng-redux .我不想使用ng-redux Has anyone tried this before?有没有人试过这个?

I am trying to migrate my angularJS app page by page.我正在尝试逐页迁移我的 angularJS 应用程序。 I have migrated the template code for a page to React component, but my controller uses few angularJS custom services.我已将页面的模板代码迁移到 React 组件,但我的控制器使用了很少的 angularJS 自定义服务。 I want to migrate this whole page to react and redux and render this page with React router.我想将整个页面迁移到 React 和 redux 并使用 React 路由器呈现此页面。 How should I proceed?我该如何进行?

Okay so there are a few things that you need to consider.好的,您需要考虑一些事项。 This process will take some time and you need to have patience.这个过程需要一些时间,你需要有耐心。

  1. Lets speak about component migration first.先说组件迁移。 For this we use the react2angular library.为此,我们使用react2angular库。 We write all our react code inside a component.js and then we create a wrapper for the angular as the library describes.我们将所有的反应代码写在一个component.js中,然后我们为库描述的角度创建一个包装器。 This is simple I believe.我相信这很简单。

  2. Moving on to pages.继续页面。 For this you can use a npm module called connected-react-router -> https://www.npmjs.com/package/connected-react-router .为此,您可以使用名为connected-react-router -> https://www.npmjs.com/package/connected-react-router的 npm 模块。 When you doing this you can also setup the store by using react-redux , redux modules.执行此操作时,您还可以使用react-reduxredux模块设置商店。

Now you should create a wrapper file for your pages where you can wrap everything inside a Provider ( here you can provide store as well, meaning the redux store to your app ) and then inside the Provider you can add your ConnectedRouter from react-connected-router npm module.现在您应该为您的页面创建一个包装文件,您可以在其中将所有内容包装在Provider中(在这里您也可以提供store ,这意味着您的应用程序的 redux 商店)然后在 Provider 中您可以从 react-connected 添加您的ConnectedRouter路由器 npm 模块。 Last inside this you can put your react pages.最后在里面你可以放你的反应页面。

An example would be一个例子是

//wrapper.js
<Provider store={store}>
          <ConnectedRouter history={history}>
            <Route path={'/somepath'} component={<Test /} />
            <Route path={'/somepath2'} component={<Test2 /} />
          </ConnectedRouter>
    </Provider>

PS React Components can access the redux-store by wrapping them with compose & connect functions from react-redux & redux . PS React 组件可以通过使用来自react-reduxreduxcomposeconnect函数包装它们来访问 redux-store。

Now you should also wrap this wrapper with a react2angular wrapper in order to integrate it into your angular application.现在你还应该用 react2angular 包装器包装这个包装器,以便将它集成到你的角度应用程序中。 In your react2angular file you should define both a component & a controller.在你的 react2angular 文件中,你应该同时定义一个组件和一个控制器。 Now the tricky part -> You need to define the same routes you added in your react-connected-route to your angular router as well and define there the template & the controller that you did in your react2angular wrapper file.现在是棘手的部分 -> 你需要定义你在你的 react-connected-route 中添加到你的角度路由器的相同路由,并在那里定义你在你的 react2angular包装文件中所做的templatecontroller

At this point you are done.此时你就完成了。 You have react pages integrated with the angular.你有与角度集成的反应页面。 During this migration you technically have 2 routers.在此迁移期间,您在技术上有 2 个路由器。 After you migrate all your pages you should create a react router put all the pages there and remove the react2angular wrappers of course + the angular router.迁移所有页面后,您应该创建一个反应路由器,将所有页面放在那里,当然要删除 react2angular 包装器 + 角度路由器。

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

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