I currently have two components, the "add-expense" component and the "view-list" component. The "add-expense" component collects expense details from a form and stores them as an object. My goal is to add this object to an empty list within the "expense-list" service. When I later load my "view-list" component, all the values in the list maintained by the "expense-list" service should be displayed.
I've read about using rxjs BehaviorSubject for this purpose. However, most examples demonstrate how to store and update strings using .next(). In my case, I need to work with a list.
import { BehaviorSubject } from "rxjs";
export class ExpenseListService{
expenseList:BehaviorSubject<any[]> = new BehaviorSubject([]); //error
}
How do I define a BehaviorSubject that takes a list as its type? How can I add/push elements to this list maintained by the BehaviorSubject? And how do I subscribe to changes?