[英]Multiple Subscription of observable does not work sometimes
我創建了一個observable
,每次button click
事件時我都在subscribing
。
import {Component} from '@angular/core';
import {MainService} from '..\services\main-service';
@component({
selector:'app-main',
style:'',
template:'<div> <button click="processing()">Processing</button></div>'
})
exports class AppMain{
constructor(private mainService: MainService){
}
processing(){
this.mainService.process$.subscribe((res)=>{
// doing something here ..
});
}
}
MainService
import {OnInit} from '@angular\core';
import {Observable} from 'rxjs\Observable';
export class MainService implements OnInit{
public process$;
constructor(){
this.process$ = new Observable((observer)=>{
// doing something here
observer.next();
});
}
}
現在,每當我多次單擊此按鈕時,有時這種可觀察到的功能就不會運行。
每次subsciption
后,我是否必須取消訂閱此可觀察項?
這是對我有效的解決方案。 我點擊此鏈接可以幫助我。
以下將解決此問題。
processing(){
const processUnsubscribe$;
processUnsubscribe$ = this.mainService.process$.subscribe((res)=>{
// doing something here ..
//Now unsubscribe here if you subscribe this multiple times.
processUnsubscribe$.unsubscribe();
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.