简体   繁体   English

如何将现有的 Angular 2 TypeScript 应用程序迁移到 ReactJS?

[英]How to migrate existing Angular 2 TypeScript app to ReactJS?

We have an existing codebase which is written in Angular 2 using TypeScript.我们有一个现有的代码库,它是使用 TypeScript 用 Angular 2 编写的。 But now we are planning to migrate the codebase to ReactJS.但是现在我们计划将代码库迁移到 ReactJS。 I am not very sure how can we do this, without rewriting the entire codebase separately from the existing code.我不太确定我们如何才能做到这一点,而无需将整个代码库与现有代码分开重写。

What I am looking for a solution that can allow us to use both Angular and React Components and their dependencies working in correlation with each other.我正在寻找一种解决方案,它可以让我们同时使用 Angular 和 React 组件及其相互关联的依赖项。

This is a painfull process but is actually doable.这是一个痛苦的过程,但实际上是可行的。 The resulting app will be eventualy unmainteneable so I suggest you to do this only for the transition to the React app refactored.生成的应用程序最终将无法维护,因此我建议您仅在过渡到重构的 React 应用程序时执行此操作。

Some examples:一些例子:

Example: Integrating angular-bootstrap-datetimepicker示例:集成 angular-bootstrap-datetimepicker

 angular .module('ngApp', ['ui.bootstrap.datetimepicker']) class AngularWidget extends React.Component { componentDidMount () { angular.bootstrap(this.container, ['ngApp']); } html = `<datetimepicker data-ng-model="data.date"></datetimepicker>`; render = () => ( <div ref={c => this.container = c} dangerouslySetInnerHTML={{__html: this.html}} /> ); }

Example: Hacking UI Router示例:入侵 UI 路由器

 componentDidMount() { angular.module('ngApp') .run(($state) => { $state.go = (state, params, options) => { console.log('hijacked ui router'); } }) angular.bootstrap(this.container, ['ngApp']); }

Example: Neutralize Angular's location tampering示例:中和 Angular 的位置篡改

 angular.module('ngApp', []) .config( ['$provide', function ($provide){ $provide.decorator('$browser', ['$delegate', function ($delegate) { $delegate.onUrlChange = function () {}; $delegate.url = function () { return ""}; return $delegate; }]); }]);

Example: Clean up angular app when component unmounts示例:卸载组件时清理 angular 应用程序

 class AngularWidget extends React.Component { componentDidMount() { this.$rootScope = angular.injector(['ng', 'ngApp']).get('$rootScope'); angular.bootstrap(this.container, ['ngApp']); } componentWillUnmount() { this.$rootScope.$destroy(); } render = () => ( <div ref={c => { this.container = c; }} dangerouslySetInnerHTML={{ __html: '<widget></widget>' }} /> ) }

Ref: https://medium.com/appifycanada/angular-components-in-react-2c929130f995参考: https : //medium.com/appifycanada/angular-components-in-react-2c929130f995

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

相关问题 如何使用 Electron 和 NodeJs 8 在 Angular 7 中通过 Jest 迁移或使用现有的业力 Jasmine - How to migrate or use existing karma Jasmine with Jest in Angular 7 with Electron and NodeJs 8 “编译失败”“解析错误:意外的令牌:”在尝试迁移到现有 create-react-app 中的 typescript 期间 - "Failed to Compile" "Parsing error: Unexpected token :" during attempt to migrate to typescript in existing create-react-app 如何从create-react-app迁移到打字稿? - how to migrate to typescript from create-react-app? 如何通过建立现有状态来更新reactjs应用程序的状态? - How to update state of reactjs app by building on existing state? 迁移现有的RequireJS应用程序以使用Webpack - Migrate existing RequireJS app to use Webpack 如何将现有的Angular 2应用程序链接到Nodejs Server? - How to link an existing Angular 2 app to Nodejs Server? 如何在ReactJS中使用TypeScript和forwardRef? - How to use TypeScript and forwardRef in ReactJS? 如何 map 和 API 与 Typescript 在 Z217C80CED5A5D142B0E105A86491709 - How to map an API with Typescript in ReactJS? 如何在reactjs中重建现有的Dom? - How to rebuild a existing Dom in reactjs? 如何在 ReactJS/Typescript 应用程序中使用 Jest 测试类中的公共异步函数 - How to test a public async function inside Class using Jest in a ReactJS/Typescript app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM