传递和展开可观察为主体的

时间:2019-11-20 14:28:24

标签: angular rxjs angular2-observables rxjs-observables

我需要将数据从一个组件传递到另一个组件,但是这样做很难。

我有一个Observable,我需要以Subject的身份通过,这是另一个“可观察的”。我怎么做? 我目前使用的方法无法正常工作,因为它已进入 target组件 undefined

这就是我所拥有的:

card.component.ts (全部从此处开始)

showBox(studentID) {
    const airportPickup = this.studentService.getCurrentStudents().pipe(
        map(snaps => {
            const student = snaps.find( s => s.studentID === studentID );
            return {
                requirePickup: student.pickup,
                whopickup: student.whoPickup
            };
        })
    );
    this.studentService.airportPickupDropoff.next(airportPickup);
}

如果我console.log() airportPickup我得到了对象。没有问题。问题是当我在其他组件中获取它时,我将其获取为undefined

flight-info.component.ts (目标)

getAirportPickup() {
    this.studentService.airportPickupDropoff.subscribe(
        (apdata) => {
            this.airportPickupDropoff = apdata;
        },
        (e) => alert(e)
    );
}

服务,我有这个:

airportPickupDropoff = new Subject<any>();

2 个答案:

答案 0 :(得分:2)

您需要订阅可观察对象,或将其与RxJS运算符(例如tap)链接起来,以便将可观察对象的值分配给'Format Measured Dim rg As Range Dim cond1 As FormatCondition, cond2 As FormatCondition, cond3 As FormatCondition Set rg = Range("G14", Range("G14").End(xlDown)) 'clear any existing conditional formatting rg.FormatConditions.Delete 'define the rule for each conditional format Set cond1 = rg.FormatConditions.Add(xlCellValue, xlGreater, "=$D14+$E14+$H14") Set cond2 = rg.FormatConditions.Add(xlCellValue, xlLess, "=$D14-$F14") 'define the format applied for each conditional format With cond1 .Font.FontStyle = "Bold" .Font.Color = vbRed End With With cond2 .Font.FontStyle = "Bold" .Font.Color = vbRed End With 主题。

选项1:

airportPickupDropoff

选项2:

showBox(studentID) {
  this.studentService.getCurrentStudents().pipe(
    map(snaps => {
      const student = snaps.find( s => s.studentID === studentID );
      return {
        requirePickup: student.pickup,
        whopickup: student.whoPickup
      };
    }),
  ).subscribe(response => {
    this.studentService.airportPickupDropoff.next(response);
  });
}

答案 1 :(得分:0)

也许最好通过Subject而不是另一个Observable发送数据?

showBox(studentID) {
  ///...
  airportPickup.subscribe(apdata => this.studentService.airportPickupDropoff.next(apdata));
}