I am encountering an issue where the subject I am trying to emit while fetching data is not triggering any events.
Even in the component's ngOnInit()
, I am unable to see the log written inside the subscription code.
The service code snippet looks like this:
@Injectable()
export class RecipeService {
recipesChanged = new Subject<Recipe[]>();
setRecipes(recipe: Recipe[]){
console.log('setrecipes() is called');
this.recipes=recipe;
console.log('Recipe list after setRecipes(): ' + recipe);
this.recipesChanged.next(this.recipes.slice());
}
Moving on to the component:
export class RecipeListComponent implements OnInit, OnDestroy {
recipes: Recipe[] = [];
subscription: Subscription;
constructor(private recipeService: RecipeService,
private router: Router, private route: ActivatedRoute) {}
ngOnInit() {
console.log('ngOnInit is called of recipe-list')
this.subscription = this.recipeService.recipesChanged
.subscribe(
(recipes: Recipe[]) => {
this.recipes = recipes;
console.log('Recipe-list recipes are: ' + recipes);
}
);
}