Meteor 如何在Tracker.autorun函数中传递上下文(this)?

Meteor 如何在Tracker.autorun函数中传递上下文(this)?,meteor,typescript,reactive-programming,Meteor,Typescript,Reactive Programming,此类将显示此=未定义的。如何传递上下文 class MeteorAccount implements IService{ constructor() { Tracker.autorun(function () { //observe Meteor.userId()); console.log(this);//undefined }); } } 您有两个选择: 委员会: 或: 编辑 还有另一个选项,我只是不喜欢它,但这是人们在使用

此类将显示此=
未定义的
。如何传递上下文

class MeteorAccount implements IService{

  constructor() {
    Tracker.autorun(function () {
      //observe
      Meteor.userId());
      console.log(this);//undefined
    });
  }

}
您有两个选择:

委员会:

或:


编辑 还有另一个选项,我只是不喜欢它,但这是人们在使用箭头功能之前的习惯(不确定为什么不使用绑定选项):


+无限点给了我使用箭头函数的真正理由!非常感谢尼赞,你让我开心@DanielN arrow函数非常有趣,一旦您习惯了它们,它就非常有用,尽管在很多情况下我仍然喜欢使用
bind
函数,因为您还可以将函数参数绑定到上下文之外的函数。
class MeteorAccount implements IService{
    constructor() {
        Tracker.autorun(function() {
            //observe
            Meteor.userId());
            console.log(this);//undefined
        }.bind(this));
    }
}
class MeteorAccount implements IService{
    constructor() {
        Tracker.autorun(() => {
            //observe
            Meteor.userId());
            console.log(this);//undefined
        });
    }
}
class MeteorAccount implements IService{
    constructor() {
        var self = this;
        
        Tracker.autorun(function() {
            //observe
            Meteor.userId());
            console.log(self);//undefined
        });
    }
}