After creating a service to share data across my entire application, I'm wondering if it's possible to append new data to an array within the userDataSource. Here is how the service looks:
user.service
userDataSource = BehaviorSubject<Array<any>>([]);
userData = this.userDataSource.asObservable();
updateUserData(data) {
this.userDataSource.next(data);
}
In my component, I make an API call and then send that data to userDataSource like this:
constructor(
private: userService: UserService,
private: api: Api
){
}
ngOnInit() {
this.api.getData()
.subscribe((data) => {
this.userService.updateUserData(data);
})
}
Everything works fine so far, but now I want to know if I can add data to the end of the array inside userDataSource without overwriting existing data. Essentially, I want to achieve something similar to using .push
. Would simply calling the updateUserData() function with additional data work in this case?
Any assistance on this matter would be greatly appreciated.