如何在Angular中使用subscribe-next RxJS?

时间:2020-05-27 03:49:32

标签: angular rxjs observable next subscribe

我正在学习rxjs。我有一个可观察到的数组。我想使用subscribe和next在功能上取得一些成果。我写了但不正确,请您帮我。非常感谢!

const fruits = from([
    "apple",
    "banana",
    "cherry"]);

fruits.subscribe(fruit => this.next(fruit));

1 个答案:

答案 0 :(得分:2)

const myObserver = {
  next: (item) => console.log('next: ', item),
  error: (err) => console.log('err: ', err),
  complete: () => console.log("Observable completed")
};

of([
  "apple",
  "banana",
  "cherry"
]).subscribe(myObserver);

setTimeout(() => myObserver.next(["Apple"]) ,2000);

锻炼的答案:D

fruits.subscribe(fruit => toConveyorBelt(fruit));