Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何停止angular 6 firestore侦听器_Angular_Firebase_Google Cloud Firestore - Fatal编程技术网

如何停止angular 6 firestore侦听器

如何停止angular 6 firestore侦听器,angular,firebase,google-cloud-firestore,Angular,Firebase,Google Cloud Firestore,这是我的代码,如果我访问其他页面,它将多次运行 例如,上面的代码在第1页,如果我访问第2页然后返回到第1页,它将调用2次,如果访问第3页并返回到第1页,它将调用3次。如何修复它 firestore.collection提供了取消订阅的方法。只需将此代码分配给变量,并在OnDestroy方法中取消订阅它,首先在组件中实现OnDestroy。更改页面时将调用ngondestory 然后您可以这样取消订阅: ngOnInit() { this.firestore.colle

这是我的代码,如果我访问其他页面,它将多次运行
例如,上面的代码在第1页,如果我访问第2页然后返回到第1页,它将调用2次,如果访问第3页并返回到第1页,它将调用3次。
如何修复它



firestore.collection
提供了
取消订阅的方法。只需将此代码分配给变量,并在OnDestroy方法中取消订阅它,首先在组件中实现OnDestroy。更改页面时将调用
ngondestory

然后您可以这样取消订阅:

 ngOnInit() {    

        this.firestore.collection('mycollection').ref.where("myfiled","==","animal") 

            .onSnapshot((data=>{}));
}
而在Ngondestory中

this.unsubscribe = this.firestore.collection("mycollection")
  .ref.where("myfiled","==","animal")
  .onSnapshot((data=>{
    // Your code
  }));

查看更多信息。

我猜您需要取消订阅,这是正确的。但是如何做到这一点呢?我不确定.onSnapShot()是否返回可观察的或订阅的,但是您可以在ngDestroy方法中执行取消订阅。将调用存储在一个变量中,然后执行variableName.unsubscribe()现在仍不工作它正在多次调用您是否在组件中实现了OnDestroy?我只能猜测此firestore的接口有问题,您能否将整个代码粘贴到某个地方?
ngOnDestroy() {
  // Stop listening to changes
  this.unsubscribe();
}