【问题标题】:How to use subscribe-next RxJS in Angular?如何在 Angular 中使用 subscribe-next RxJS?
【发布时间】:2020-09-14 00:31:19
【问题描述】:

我正在学习 rxjs。我有一个数组中的 observable。我想在函数中添加一些水果,使用 subscribe 和 next。我写了但不对,我可以帮助你。非常感谢!

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

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

【问题讨论】:

    标签: angular rxjs observable next subscribe


    【解决方案1】:
    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));
    

    【讨论】:

    • 谢谢!我想完成这节课:rxjs-fruits.com/subscribe-next 但我不知道观察者这节课有什么?你能给我解释一下吗?再次感谢!!!
    • 如果它适合您,我建议您投票/接受答案。 :)
    • @GiangGiang,请为答案投票。我在上面的课程中添加了解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    相关资源
    最近更新 更多