0

In my application, I've 2 apps on the same page - one is Angular app and the other is a React app.

The Angular app is the sidebar and the React app is main component. Both of these apps need information from each other. Is this possible?

I'd want to avoid using local storage or server side for this as the shared information is very dynamic and changes frequently(e.g - clicking a button in angular app triggering behaviour in React app).

The reason I've 2 apps on the same page is because I'm migrating a bigger angular application to react. But the whole thing has active users and I'm doing it in components.

If it's possible, it'd be very helpful to get some examples. Thanks!

1
  • Use global variables (temporary solution) Commented Jan 7, 2019 at 9:00

1 Answer 1

5

If you want to share data (in-memory), you can always use window variable.

like

in App 1: window.someVar = "hello"
in App 2: console.log(window.someVar); 

for angular you might want to deceive the ts transpiler by declaring window on top.

declare var window; 

OR

If you want to communicate on event basis, dispatch and listen to events

Dispatch event (App 1):

  let evt = new CustomEvent('InterAppData', { data: "hello" });
  window.dispatchEvent(evt);

Subscribe to event (App 2):

window.addEventListener('InterAppData', (e) => {
  if (e.data) {
    console.log(e.data); //--> prints "hello"
  }
});

PS: Here App 1 and App 2 can be React and Angular or Vice-Versa.

Sign up to request clarification or add additional context in comments.

1 Comment

This needs to be detail: "hello", rather than data: "hello". and e.detail, rather than e.data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.