【问题标题】:Angular and FabricJS class variables and eventsAngular 和 FabricJS 类变量和事件
【发布时间】:2019-07-01 11:19:05
【问题描述】:

我需要从事件处理程序访问我的类变量,但它不起作用。我尝试使用 this.i,但它也不起作用;

export class FirstClass{    
  i:number;

  circle:any;

  constructor(){

    circle = new fabric.Circle({left:100,top:100,radius:30});

    circle.on("mousedown",function(){i++;});
  }
}

【问题讨论】:

  • thisarrow function 救援

标签: angular fabricjs


【解决方案1】:

circle.on("mousedown",function(){i++;}); 更改为circle.on("mousedown",function(){this.i++;});。任何对“顶级”变量(在类中但不在本地函数中)的引用都必须通过this. 引用。

Typescript 是强类型的 Javascript,这意味着您不能像在代码中尝试的那样松散地引用变量。

【讨论】:

  • 我想当我像你一样使用这个时,这将指代圆圈而不是 FirstClass。
  • 在类中使用this. 将引用类的“顶级”上的任何内容,这意味着在构造函数中执行this. 将允许您使用this.ithis.circle跨度>
  • 我不确定因为类似的东西改变了圆的填充:constructor(){ this.circle.on("mouse:down",function(){this.set("fill","红色的”)});在这种情况下,在事件处理函数中,this 指的是类的圈号。
猜你喜欢
  • 2019-07-01
  • 2019-12-28
  • 1970-01-01
  • 2018-07-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
  • 2019-09-23
  • 2012-01-08
相关资源
最近更新 更多