我有一个管理特定对话框的单独对象。考虑下面的代码。因为很难想象函数的作用,但是我无法访问该类的实例。我尝试使用传统that = this
方法。
export class Whatever implements OnInit {
that = this;
dialog = {
data:{},
open:function() {
//way to access 'that' variable
},
close:function() {},
toggle:function() {}
}
//other declarations and functions
}
当我的应用程序扩展时,该服务中有太多功能。因此,我试图将这些功能中的一些整合到对象中,这将使代码更清晰。
另外,如果有更好的方法,我也想知道。谢谢。
最好的方法是function(){}
用ES6箭头函数代替,该函数可以this
像这样保持您的上下文() => {}
。
您也可以使用functions(){}.bind(this)
,但是仅使用箭头功能会更好。两者都会使您this
在函数主体中按预期进行引用
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句