【问题标题】:Typescript: access this from class scope and this from function scope打字稿:从类范围访问 this 和从函数范围访问 this
【发布时间】:2016-07-13 12:48:11
【问题描述】:

我有一个带有点击监听器的 svg 组件:

export class test {
    param: any;

    //...
    .on("click", () => {
        this.param = this.__data__;
    });
}

我想从类测试中访问参数(如上所示)并从点击上下文的范围内访问this.__data__... 我试图在课堂上定义self = this 和其他东西,但无法让它工作......

【问题讨论】:

    标签: javascript typescript angular scope onclick


    【解决方案1】:

    在这种情况下,我认为你需要使用fat函数:

    let self = this;
    .on("click", function() {
      self.param = this.__data__;
    });
    

    【讨论】:

      【解决方案2】:

      这里是 Thierry Templier 的 the valid solution 的替代品,也许您可​​以使用 event.currentTarget

      el.on("click", (evt) => {
          this.param = evt.currentTarget.__data__;
      });
      

      我不知道在您的示例中是谁提供了el。但是currentTarget 存在in DOM eventsin JQuery events

      【讨论】:

        猜你喜欢
        • 2013-06-12
        • 1970-01-01
        • 2021-10-16
        • 2016-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-23
        相关资源
        最近更新 更多