我试图取消订阅ngOnDestroy中的一个observable,但它告诉我它在Observable类型中不存在。
export class AppComponent implements OnInit {
public caseData;
constructor(private _caseService: CaseService,
private _title: Title,
private _metaService: Meta) { }
ngOnInit(): void {
this._caseService.GetCaseData('en', 'English');
this._caseService.caseData$.subscribe(res => { this.caseData = res },
(error) => { console.log(error) });
}
ngOnDestroy() {
this._caseService.caseData$.unsubscribe(); //says it does not exist
}
}
答案 0 :(得分:7)
订阅本身已退回。
subscription: Subscription;
ngOnInit(): void {
this._caseService.GetCaseData('en', 'English');
this.subscription = this._caseService.caseData$.subscribe(res => { this.caseData = res },
(error) => { console.log(error) });
}
ngOnDestroy(){
this.subscription.unsubscribe();
}
答案 1 :(得分:2)
unsubscribe()
是Subscription
的方法,而不是Observable
。
答案 2 :(得分:0)
这应该有效:
export class AppComponent implements OnInit {
public caseData;
subscription: any;
constructor(private _caseService: CaseService, private _title: Title, private _metaService: Meta) { }
ngOnInit() {
this._caseService.GetCaseData('en', 'English');
this.subscription = this._caseService.caseData$.subscribe(res => { this.caseData = res },
(error) => { console.log(error) });
}
ngOnDestroy(){
this.subscription.unsubscribe();
}
}
答案 3 :(得分:0)