I'm currently working on sharing the date between components using BehaviorSubject, but I'm encountering an error in the process.
public data = new BehaviorSubject<any>(this.selectedValue);
public sharedData = this.data.asObservable();
selectedValue: Meal[] = [];
fetchSubCategoriesFood(name: string): Observable<{ meals: Meal[] }> {
return this.http
.get<{ meals: Meal[] }>(
this.recipes.listOfSubcategories + 'filter.php?i=' + name
)
.pipe(
tap((result) => {
this.selectedValue = result?.meals;
})
);
}
Here's how I am subscribing to the service in another component:
This is my secondary Component where I am listening for changes.
export class CategoryPageComponent implements OnInit {
constructor(public categoryService: HomepageService) {}
ngOnInit(): void {
const subscription = this.categoryService.sharedData.subscribe((data) =>
console.log(data)
);
}
}