Angular 财产';取消订阅';不存在于类型';可观察<;DataSnapshot>';
Typescript(atom编辑器中的tslint)给了我一个Typescript错误,但我不知道如何设置正确的类型 错误消息: 聊天组件: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
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();