【问题标题】:not getting value from getRecipe()没有从 getRecipe() 获得价值
【发布时间】:2020-08-06 07:10:59
【问题描述】:

我试图在从另一个服务获取数据时发出主题,但主题没有将值从 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();
    
    }





  

在配方列表组件中

   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() 日志中我得到了价值,但它没有设置我的食谱 [] 的值,因此它没有将值传递给组件。

【问题讨论】:

    标签: angular typescript rxjs subject-observer


    【解决方案1】:

    您的主题发出的 setRecipes 值是空数组 recipes 变量,并且您的 recipes 数组永远不会被修改。

    所以 getRecipes 将始终返回一个空数组。

    如果你发出带有主题的食谱列表也有点奇怪,为什么你需要一个 getRecipes 方法?您应该简单地发出传递给 setRecipes 函数的食谱列表

      setRecipes(recipe: Recipe[]){
          this.recipesChanged.next(recipe.slice());
      }
    

    在你的组件中

    ngOnInit() {
        this.recipeService.recipesChanged
        .subscribe(
            (recipes: Recipe[]) => {
                this.recipes = recipes;     
            }
        );
    }
    

    如果您希望将食谱列表保存在您的服务中,那么您可以执行类似的操作

    @Injectable()
    export class RecipeService{
    recipesChanged = new BehaviourSubject(false);
    private recipes:Recipe[]=[];
    
    constructor(private slService:ShoppingListService){}
    
    setRecipes(recipe: Recipe[]){
        this.recipes = recipe;
        this.recipesChanged.next(true);
    }
    
    getRecipes(){
        return this.recipes.slice();        
    }
    

    在你的组件中

    ngOnInit() {
        this.recipeService.recipesChanged
        .subscribe(
            hasChanged => {
                if (hasChanged)
                this.recipes = this.recipeService.getRecipes();     
            }
        );
    }
    

    【讨论】:

    • setrecipe() 从另一个服务调用,其中来自 DB 的值正在被传递。但主题没有从 setRecipe().fetchRecipes(){ return this.http.get("//API URL") .subscribe (recipes => { this.rService.setRecipe(recipes) // 传入Recipes数组的值 }); }
    • 当我记录值时,我在 setRecipe() 中获取值,但在将其存储在 recipes[] 中后,它没有进入 getRecipe()。
    • 在您的代码配方数组中初始化为空,并且您永远不会在其中推送任何数据。所以 getRecipe 不能返回这个空数组以外的任何东西。
    猜你喜欢
    • 2016-10-20
    • 2018-04-08
    • 2012-06-24
    • 2021-10-31
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    相关资源
    最近更新 更多