rxJS에서 구독 및 관찰 가능 항목을 파악하려고합니다.
구독을 취소하여 Observable의 간격을 변경 한 다음 새 간격 설정을 사용하여 다시 구독하고 싶습니다.
정말 간단해야하지만이 분야의 초보자이기 때문에 도움이 필요할 수 있습니다.
이 플렁크 보기
export class AppComponent implements OnInit {
title = 'Observable Interval - Changing interval';
currentTime: any;
refreshInterval: number = 1000;
private subscription: Subscription;
constructor(private timeService: TimeService) {
}
clicked($event) {
console.log('new refreshInterval: ' + this.refreshInterval);
// Here I would like to unsubscribe to the subscription
// and then resubscribe using the new interval.
// However using below statement causes a
// TypeError: Cannot read property 'unsubscribe' of undefined
this.subscription.unsubscribe();
this.getTime();
}
// with this implementation changing the refreshInterval won't have any affect.
getTime() {
this.timeService.getTime(this.refreshInterval)
.subscribe(t => {
this.currentTime = t;
}
);
}
ngOnInit() {
this.subscription = this.getTime();
console.log(this.subscription);
console.log('refreshing each ' + this.refreshInterval + ' ms');
}
}
getTime
메서드 내에서 구독을 반환해야합니다 .
getTime() {
return this.timeService.getTime(this.refreshInterval) // <-----
.subscribe(t => {
this.currentTime = t;
}
);
}
귀하의 경우에는 아무것도 반환되지 않습니다. 그래서 정의되지 않은 오류가 있습니다 ...
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다