简体   繁体   English

交流React <-> Angular Iframe

[英]Communicate React<->Angular Iframe

I know people will say don't do this or why use both.. but I have to. 我知道人们会说不要这样做或为什么要同时使用这两种方法..但是我必须这样做。

I need to pass data from React(child) to Angular (parent) to React(child) in an iframe. 我需要在iframe中将数据从React(child)传递到Angular(父)再传递到React(child)。

So far I can use window.postMessage from React to angular with @HostListener , the issue is that (and I think) is that the window attribute is getting refreshed every time I use postMessage . 到目前为止,我可以使用@HostListener从React使用window.postMessage到angular,问题是(并且我认为)是每次我使用postMessage时,窗口属性都会刷新。 Within React, there are components that use componentDidUpdate and this causes an entire refresh within react component once I click/select within react. 在React内,有一些组件使用componentDidUpdate ,一旦我在react中单击/选择,这将导致react组件内的整个刷新。

I can't do react to react because there are things that needs to be passed up to angular parent. 我无法做出反应,因为有些事情需要传递给有角的父母。

在此处输入图片说明

React side: 反应方:

//some item is selected in react
onSelect = selected => {
    window.parent.postMessage(
      {
        activeData:tselected
      },
      '*'
    )
  }

sends to angular in iframe 发送到iframe中的angular

@HostListener('window:message', ['$event'])
    onMessage(e) {
      this.selection = e['data'];
  }

output looks great but then the entire window gets refreshed to original blank state once it is selected. 输出看起来不错,但是一旦选中它,整个窗口就会刷新为原始空白状态。

I can see a lot of advantages of using Angular & ReactJS in one project. 我可以看到在一个项目中使用Angular和ReactJS的许多优点。 You can simple divide code, use Angular as a router & parent container, and implement React components on demand. 您可以简单地划分代码,将Angular用作路由器和父容器,并按需实现React组件。 It's very good idea. 这是个好主意。 In old project you can in this way have a little innovation and use different technologies so you can develop new sills. 在旧项目中,您可以通过这种方式进行一些创新,并使用不同的技术,以便开发新的窗台。

The easiest answer on your question how to pass data between angular & react is to use RxJS and subscription or Redux. 关于如何在angular和react之间传递数据的问题,最简单的答案是使用RxJS和subscription或Redux。 In this way you can use one Redux store in few application, or pass one BehaviorSubject. 这样,您可以在少数几个应用程序中使用一个Redux存储,或传递一个BehaviorSubject。

I'm not recommend you using events and post.messages for communication. 我不建议您使用eventspost.messages进行通信。 It's unnecessary and you should support all kind of problems. 这是不必要的,您应该支持所有类型的问题。 Why if you have a ready solutions like RxJS or Redux? 为什么要有RxJS或Redux等现成的解决方案?

If you use RxJS in ReactJS you can simply use comopnentDidMount() with subscription, like this: 如果在ReactJS中使用RxJS,则可以简单地将comopnentDidMount()与预订一起使用,如下所示:

  componentDidMount(): void {
    // In componentDidMount we subscribe heroes$ object
    this.props.heroes$.subscribe((res: IHero[]) => {
      // and we pass this data into React State object
      this.setState({heroes: res});
    });
  }

If you look for demo, you can get it from github where I create repo with minimal version of communication between Angular 7 app and React 16 如果您需要演示,可以从github上获得它,我在Angular 7应用程序和React 16之间以最小的通信版本创建了仓库

If you need 2 separate React implementation it's not a problem to extend presented solutions. 如果您需要2个独立的React实现,则扩展现有解决方案不是问题。 Look also hire: Connect Angular application with ReactJS app? 还要看一下将Angular应用程序与ReactJS应用程序连接起来吗?

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

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