代码之家  ›  专栏  ›  技术社区  ›  Miquel Angel Barragan Laso

向Observable<Object[]>变量添加元素

  •  0
  • Miquel Angel Barragan Laso  · 技术社区  · 6 年前

    可观察列表:

    profesionales$: Observable<Profesional[]>;
    

     this.profesionales$ = this.profesionales$.pipe(map(profesionales => {
         return [...profesionales, profesional];
     }));
    

    […professionales,professional]中的最后一个“professional”是add to The list的元素。

    谢谢。

    0 回复  |  直到 6 年前
        1
  •  0
  •   Carlos Ferras    6 年前

    我建议不要使用Observable,而应该使用BehaviorSubject并这样使用:

    profesionales$: BehaviorSubject<Profesional[]> = new BehaviorSubject<Profesional[]>([]);
    

    this.profesionales$.next([...this.profesionales$.getValue(), newProfesional])
    
        2
  •  0
  •   Nathan Beck    6 年前

    .mergeMap of 添加到可观察的。

    let profesional = { id: 1, name: "some example profesional" };
    
    this.profesionales$ = this.http
        .get<Profesional[]>("some_url")
        .pipe(mergeMap(v => of(v.concat(profesional));
    

    http 作为一个可观察的例子调用,因为我不确定您的实现。这也很有用--> learn rxjs docs