Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.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 财产';取消订阅';不存在于类型';可观察<;DataSnapshot>';_Angular_Typescript_Ionic Framework_Rxjs_Tslint - Fatal编程技术网

Angular 财产';取消订阅';不存在于类型';可观察<;DataSnapshot>';

Angular 财产';取消订阅';不存在于类型';可观察<;DataSnapshot>';,angular,typescript,ionic-framework,rxjs,tslint,Angular,Typescript,Ionic Framework,Rxjs,Tslint,Typescript(atom编辑器中的tslint)给了我一个Typescript错误,但我不知道如何设置正确的类型 错误消息: 聊天组件: private _chatObserver: Observable<firebase.database.DataSnapshot> otherMethod () { this._chatObserver = this._chat.observe(alarmId) this._chatObserver

Typescript(atom编辑器中的tslint)给了我一个Typescript错误,但我不知道如何设置正确的类型

错误消息:

聊天组件:

  private _chatObserver: Observable<firebase.database.DataSnapshot>

  otherMethod () {
        this._chatObserver = this._chat.observe(alarmId)
        this._chatObserver.subscribe(
          (messageSnap: firebase.database.DataSnapshot) => {
            this.messages.push(messageSnap.val())
          },
          error => {throw error})
    }

    ionViewDidLeave() {
       this._chatObserver.unsubscribe() 
    }
试试这个

private _chatObserver: Observable<firebase.database.DataSnapshot>
private _subscription:Subscription
otherMethod () {
    this._chatObserver = this._chat.observe(alarmId)
    this._subscription=this._chatObserver.subscribe(
      (messageSnap: firebase.database.DataSnapshot) => {
        this.messages.push(messageSnap.val())
      },
      error => {throw error})
}

ionViewDidLeave() {
   this._subscription.unsubscribe() 
}
private\u chatObserver:可观察
私人订阅:订阅
其他方法(){
this.\u chatObserver=this.\u chat.observe(alarmId)
this.\u subscription=this.\u chatObserver.subscripte(
(messageSnap:firebase.database.DataSnapshot)=>{
this.messages.push(messageSnap.val())
},
错误=>{throw error})
}
ionViewDidLeave(){
此.\u订阅。取消订阅()
}

取消订阅
是订阅本身的一种方法。因此,在顶部添加:

private _chatSubscription;
然后在您的
其他方法中

this._chatSubscription = this._chatObserver.subscribe(...);
在销毁/离开/终止处理程序中:

this._chatSubscription.unsubscribe();

取消订阅
应在
订阅
上调用-而不是在
可观察的
上调用。您对
subscribe
的呼叫将返回
subscribe
this._chatSubscription.unsubscribe();