My Angular 8 project features two sibling components that utilize a service to manage restaurant data. One component displays a list of restaurants fetched from an api, while the other component filters the displayed restaurants based on user input. Despite using a service to handle restaurant data, I am facing an issue where the data is not being populated upon loading. Below is the code for my components and service:
Restaurant.service.ts
@Injectable()
export class EatstreetService {
radius;
address;
public restaurants: BehaviorSubject<any> = new BehaviorSubject<any>(null);
constructor(private httpClient: HttpClient) { }
public setRestaurant(){
let params = new HttpParams();
params = params.append('method', 'delivery');
params = params.append('pickup-radius', this.radius);
params = params.append('street-address', this.address);
params = params.append('access-token', this.token)
this.restaurants.next(this.httpClient.get(`https://api.com/publicapi/v1/restaurant/search`, {params}))
}
public getRestaurant(): Observable<any>{
return this.restaurants.asObservable();
}
}
RestaurantFilter.component.ts
export class LocationComponent implements OnInit {
restaurants;
radius;
address;
constructor(private eatstreetService: EatstreetService) { }
setRestaurants() {
this.eatstreetService.setRestaurant()
}
getRestaurants() {
this.eatstreetService.getRestaurant().subscribe((data) => {
console.log(data['restaurants']);
this.restaurants = data['restaurants'];
})
}
onSelect(): void {
this.setRestaurants()
}
ngOnInit() {
this.setRestaurants()
this.getRestaurants()
}
}
RestaurantsList.component.ts
export class CardsComponent implements OnInit {
restaurants;
constructor(private eatstreetService: EatstreetService) { }
getRestaurants() {
this.eatstreetService.getRestaurant().subscribe((data) => {
console.log(data['restaurants']);
this.restaurants = data['restaurants'];
})
}
setRestaurants() {
this.eatstreetService.setRestaurant()
}
ngOnInit() {
this.getRestaurants()
}
I have researched similar issues, but none of the suggested solutions seem to work for me.