Ionic rxjs管道函数在http发布后没有被调用。

问题描述 投票:0回答:1

我使用的是ionic 5

当我尝试使用post连接到firebase,获取响应数据,并使用 水管和水龙头 那么它就不能工作了。日志没有打印。

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

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

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

工作代码

 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);
    })
  );
firebase ionic-framework rxjs rxjs6
1个回答
1
投票

正如评论中所说,你必须调用 订阅 办法

这里是再现你的案例并提供上述解决方案的片段。

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>
© www.soinside.com 2019 - 2024. All rights reserved.