How do I share data between sibling components in angular

0 votes
How do I share data between sibling components in angular?

I'm trying to share data between sibling components in my Angular application. Can someone help me with the best approach to achieve this?"

Let me know if you'd like examples or details about using services, @Input(), @Output(), or any other method!
Dec 12, 2024 in Web Development by Nidhi
• 5,440 points
57 views

1 answer to this question.

0 votes

 

  1. Behavior Subject is a unique RxJS subject that stores the most recent information and enables component subscription to updates.
  2. Shared Service: Facilitates communication across sibling components by serving as a central repository for data.
  3. Subscription: When the data changes, components that have signed up for the service will automatically receive updates.

Shared Service:

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class SharedDataService {
  private dataSubject = new BehaviorSubject<any>(null); // Initial value is null
  data$ = this.dataSubject.asObservable(); // Observable to share data

  updateData(data: any) {
    this.dataSubject.next(data); // Method to update data
  }
}

 Component 1 (Sender):

import { Component } from '@angular/core';
import { SharedDataService } from '../shared-data.service';

@Component({
  selector: 'app-component1',
  template: `
    <button (click)="shareData('cmp2')">Go to Comp 2</button>
    <button (click)="shareData('cmp3')">Go to Comp 3</button>
  `,
})
export class Component1Component {
  data = { api: 'https://api.example.com/discounts' }; // Sensitive data

  constructor(private sharedService: SharedDataService) {}

  shareData(route: string) {
    this.sharedService.updateData(this.data); // Update data in the service
  }
}

Component 2 (Receiver):

import { Component, OnInit } from '@angular/core';
import { SharedDataService } from '../shared-data.service';

@Component({
  selector: 'app-component2',
  template: `Data: {{ data | json }}`,
})
export class Component2Component implements OnInit {
  data: any;

  constructor(private sharedService: SharedDataService) {}

  ngOnInit() {
    this.sharedService.data$.subscribe((data) => (this.data = data)); // Subscribe to the data
  }
}

Routing Setup:

const routes: Routes = [
  { path: 'cmp1', component: Component1Component },
  { path: 'cmp2', component: Component2Component },
  { path: 'cmp3', component: Component3Component },
];

answered Dec 12, 2024 by Navya

Related Questions In Web Development

0 votes
1 answer

How do you detect memory leaks in Angular and fix them?

Imagine your computer is a room and ...READ MORE

answered Oct 25, 2024 in Web Development by kavya
180 views
0 votes
0 answers

How do I chain asynchronous AJAX calls with promises in jQuery to ensure proper order when modifying the DOM?

How do I chain asynchronous AJAX calls ...READ MORE

Nov 13, 2024 in Web Development by Nidhi
• 5,440 points
146 views
0 votes
0 answers

How do I fix performance issues caused by event delegation in a large DOM tree using jQuery?

How do I fix performance issues caused ...READ MORE

Nov 19, 2024 in Web Development by Nidhi
• 5,440 points
61 views
0 votes
1 answer

How do you handle asynchronous actions in Redux with Redux-Saga for complex data fetching scenarios?

To handle asynchronous actions in Redux using ...READ MORE

answered Dec 12, 2024 in Web Development by Navya
52 views
+1 vote
8 answers

How can I implement process.env in Angular 5 environment?

Users do not have access to process.env ...READ MORE

answered Apr 3, 2018 in DevOps & Agile by DareDev
• 6,890 points
13,291 views
0 votes
1 answer
0 votes
4 answers

ReactJS vs Angular Comparison: Which is better?

Parameters React Angular Type React is a JavaScript library, and it ...READ MORE

answered Jan 7, 2021 in Events & Trending Topics by Focusteck
• 140 points
1,880 views
+4 votes
9 answers

***IMPORTANT*** AngularJS Interview Questions.

Yes, I agree with Omkar AngularJs is ...READ MORE

answered Mar 17, 2019 in Career Counselling by Sharad
• 180 points
3,790 views
0 votes
1 answer

How to update data between parent and child components in Angular?

Data between parent and child components can ...READ MORE

answered Nov 27, 2024 in Web Development by kavya
68 views
0 votes
1 answer

How do you detect memory leaks in Angular and fix them?

Detecting and fixing memory leaks in an ...READ MORE

answered Dec 12, 2024 in Web Development by Navya
56 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP