I'm feeling a bit lost here...
So, I have two sibling components. The first component fetches some data and I want to share that data with the second component, so I created a service:
Data Sharing Service
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class DataService {
private dataSource = new BehaviorSubject<Object>({});
data = this.dataSource.asObservable();
updateData(data) {
this.dataSource.next(data);
}
}
In my first component...
this.activatedRoute.params.subscribe(params => {
this.id = params.id;
this._dataNotification.getSingledataNotification(this.id)
.subscribe(result => {
this.data = result;
this._dataService.updateData(this.data);
});
});
And in my second component...
getData() {
this._dataService.data.subscribe((result) => {
this.data = result;
console.log(this.data);
});
}
The issue is that even though I pass data from the first component to the service and then try to access it in the second component, I only see an empty object when I log it. What am I missing?
UPDATE
After moving the updateData function to an onChanges lifecycle event, everything seems to be working fine now.
Thanks for your help!