【问题标题】:Ionic rxjs pipe function not getting called after http post离子 rxjs 管道函数在 http 发布后没有被调用
【发布时间】:2020-05-18 02:09:29
【问题描述】:

我正在使用离子 5

当我尝试使用 post 连接到 firebase 并获取响应数据并使用 pipe and tap 处理响应时,它无法正常工作。日志未打印。

但是当我用订阅替换管道时,它工作正常,我可以正确看到日志。

请查看下面的工作代码和不工作代码。

谁能帮我解决这个问题。感谢您的帮助。

工作代码

 return this.http.post("https://project-name.firebaseio.com/offered-places.json", {
    ...newPlace, 
    id: null
  }).subscribe(resDate => {
    console.log(resDate);
  });

代码无效

return this.http.post("https://project-name.firebaseio.com/offered-places.json", {
    ...newPlace, 
    id: null
  }).pipe(
    tap(resData => {
      console.log(resData);
    })
  );

【问题讨论】:

  • 你必须订阅才能执行一个 observable
  • 关于您的问题的更多细节:如果您有 0 个订阅者正在监听您的 observable,那么您的 observable 会获取发出的值,但不会将其推送到管道中。这意味着:0 个订阅 === 没有执行管道操作。
  • 感谢您的指挥。您能告诉我如何将订阅者添加到可观察对象,以便它可以将值推送到管道。谢谢

标签: firebase ionic-framework rxjs rxjs6


【解决方案1】:

如 cmets 中所述,您必须调用 subscribe 方法

这是重现您的案例并提供上述解决方案的 sn-p。

const exampleObservable1 = rxjs.of([{}]);
const exampleObservable2 = rxjs.of([{}]);
const exampleObservable3 = rxjs.of([{}]);

console.log('working example');
exampleObservable1.subscribe(resDate => {
  console.log(resDate);
});

console.log('not working example');
exampleObservable2.pipe(
  rxjs.operators.tap(resData => {
    console.log(resData);
  }))

console.log('suggestion');
exampleObservable3.pipe(
  rxjs.operators.tap(resData => {
    console.log('tap', resData);
  })).subscribe(resDate => {
  console.log('subscription', resDate);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.5/rxjs.umd.js"></script>

【讨论】:

  • 谢谢 这按预期工作。这是我的离子代码中的代码 return this.http.post("project-name.firebaseio.com/offered-places.json", { ...newPlace, id: null }) .pipe( tap(resData => { console. log("D" + resData); })).subscribe(resData => { console.log("订阅中的数据:" + resData) });
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-30
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
相关资源
最近更新 更多