我订阅了一个方法中的一个observable,需要在另一个方法中取消订阅。从init函数调用subCounter()方法,内容工作得很好。
subCounter() {
this.fml = this.playerService.counter(this.song).subscribe(data => {
this.pos = data;
this.time.position = Math.round(this.pos);
this.time.dur = this.song.duration - this.time.position;
this.time.durMinutes = Math.floor(this.time.dur / 60);
this.time.durSeconds = ('0' + Math.ceil(this.time.dur - this.time.durMinutes * 60)).slice(-2);
this.time.posMinutes = Math.floor(this.time.position / 60);
this.time.posSeconds = ('0' + Math.ceil(this.time.position - this.time.posMinutes * 60)).slice(-2);
this.time.percent = this.time.position / this.song.duration * 100;
})
// This works just fine
console.log(this.fml);
}
当我调用touchActivate()函数时,它包含一个存储的变量的取消订阅函数,但是由于this.fml.unsubscribe
未定义,它会抛出错误。控制台日志记录this.fml
在未定义的对象中返回。
touchActivate() {
console.log(this.fml);
this.fml.unsubscribe();
}
在我班级的顶部,我定义了变量:
public fml: any;
答案 0 :(得分:1)
试试这个:
$('.range-slider').on("touchstart", this.touchActivate.bind(this));
或使用箭头功能保留this
:
$('.range-slider').on("touchstart", () => this.touchActivate());