【问题标题】:Add elements to rxjs BehaviorSubject or Subject of an array in Angular2+在Angular 2中将元素添加到数组的rxjs BehaviorSubject和Subject
【发布时间】:2018-09-18 17:15:31
【问题描述】:

我正在阅读教程here 的“不相关组件:与服务共享数据”部分中有关如何在 Angular 中不相关组件之间共享数据的信息。

我看到了这个示例如何适用于他们试图在其组件之间共享的字符串,但我的数据类型有点复杂:

也就是说,我认为我的 BehaviorSubject 应该是这样的:

private currentPopulationSource: BehaviorSubject<Population> = new BehaviorSubject<Population>(new Population(new Array<Organism>()));

我的种群模型只是一个有机体数组的容器:

import { Organism } from './organism.model';

export class Population {
  private individuals: any;
  constructor(individuals: Organism[]){
     this.individuals = individuals;
  }

  getIndividuals(){
    return this.individuals;
  }
}

我有一个有机体实例,我们称之为有机体1。

我想将它添加到包含在 Population 模型中的个人数组中,并且我希望多个不相关的组件订阅人口 BehaviorSubject(我目前在我的 PopulationManagerService 中有 private currentPopulation = this.currentPopulationSource.asObservable(); 在我声明之后currentPopulationSource,正如我在教程中看到的那样)。

我不清楚将有机体1 添加到我的 currentPopulationSource 的语法是什么(.next() 在这里似乎没有意义)。

如果我希望发出一个不断增长的数组,也许 BehaviorSubject 不是最合适的选择?如果有更好的选择(ReplaySubject?),我不太清楚如何实现。

我的人口管理服务:

import { Injectable } from '@angular/core';
import { Organism } from './organism.model';
import { Population } from './population.model';
import { BehaviorSubject } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class PopulationManagerService {
  private currentPopulationSource: BehaviorSubject<Population> = new BehaviorSubject<Population>(new Population(new Array<Organism>()));
  currentPopulation = this.currentPopulationSource.asObservable();
  constructor() { }

  addOrganismToPopulation(organism: Organism){
    this.currentPopulationSource.next(new Population(new Array<Organism>(organism))); //This does not work
    // this.currentPopulation.getIndividuals().push(organism); //This did not work either, because currentPopulation is of type Observable<Population> rather than of type Population
  }
}

在我的组件中:

let testIndividual: Organism = this.individualGenService.makeIndividual("green", "blue");
    this.popManager.addOrganismToPopulation(testIndividual);
    this.popManager.currentPopulation.subscribe(results =>{
      console.log(results.getIndividuals()); //returns undefined
    });

目前返回未定义。

非常感谢您对此问题的任何帮助。

【问题讨论】:

  • 问题中的噪音太大。你可以创建一个stackblitz吗?

标签: angular service rxjs behaviorsubject


【解决方案1】:

如果我理解正确,您想将一个新的有机体添加到人口对象内的有机体列表中。这在使用行为主题时。

在您的示例中,您可以执行以下操作。

addOrganismToPopulation(organism: Organism){
    this.currentPopulationSource
        .pipe(take(1))
        .subscribe((population: Population) => {
            this.currentPopulationSource.next(
                new Population([...population.getIndividuals(), organism]))
            )
        });
  }

那么我们在这里做什么。要将新生物添加到当前种群中,我们需要知道生物列表。所以我们订阅了持有人口的 observable。在订阅中,我们创建了一个新的人口实例。在创建新实例时,我们会创建一组已知生物体和新生物体。然后我们将新的/更新的人口放到流中。

请注意,我只取流的一个值,take(1)。这是因为当我们想要计算新的生物列表时,我们只需要当前的种群。这也可以防止不必要的内存泄漏。 take 运算符在一个事件过去后取消订阅流。

用最少的信息很难说行为主题是否适合您的用例。

【讨论】:

  • 谢谢,@Ruben!你理解正确。我还用我的 Population 构造函数修复了一个错误(上面已编辑)
猜你喜欢
  • 2016-08-10
  • 2017-02-02
  • 2017-10-27
  • 1970-01-01
  • 2019-03-28
  • 2017-04-21
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
相关资源
最近更新 更多