Я пытаюсь передать тему при получении данных из другой службы, но субъект не передает значение из setRecipe () в getRecipe (), а в ngOninit компонента нет значения, получаемого от службы.
@Injectable()
export class RecipeService{
recipesChanged = new Subject<Recipe[]>();
private recipes:Recipe[]=[];
constructor(private slService:ShoppingListService){}
setRecipes(recipe: Recipe[]){
this.recipesChanged.next(this.recipes.slice());
console.log("value from setRecipe()", this.recipes);
}
getRecipes(){
console.log("value from getRecipes()", this.recipes.slice());
return this.recipes.slice();
}
в RecipeListComponent
export class RecipeListComponent implements OnInit, OnDestroy {
recipes:Recipe[];
subscription: Subscription;
constructor(private recipeService : RecipeService ,
private router : Router ,private route : ActivatedRoute
) {}
ngOnInit() {
this.recipeService.recipesChanged
.subscribe(
(recipes: Recipe[]) => {
console.log("recipes from recipe list comp",recipes);
this.recipes = recipes;
}
);
console.log("my recipes",this.recipes);
this.recipes= this.recipeService.getRecipes();
}
и данные извлекаются из firebase в службе хранилища данных и устанавливаются в setRecipe () службы рецептов. Я получаю значение рецептов из Firebase в службу хранилища данных, а в журнале setRecipe () я получаю значение, но оно не устанавливает значение моих рецептов [] и поэтому не передает значение компоненту.