Hel*_*rto 0 javascript class function typescript angular
我尝试以这种方式将参数“this”传递给函数:
modalshow = new Confirmation(false, this.changea(this), this.changer);
Run Code Online (Sandbox Code Playgroud)
和确认类是这样的:
constructor(
public show: boolean,
public actionAccept: Function,
public actionReject: Function
) {}
Run Code Online (Sandbox Code Playgroud)
但我不断收到错误:
“void”类型的参数不能分配给“Function”类型的参数
所以,我不知道是什么问题,我应该怎么做。
您传递的第二个参数是this.changea(this)- 结果是您传递的是 techangea函数的返回值,而不是函数本身。
如果要将函数作为参数传递,并保留 的含义this,可以使用:
modalshow = new Confirmation(false, () => this.changea(this), this.changer);
Run Code Online (Sandbox Code Playgroud)
您现在已将代码包装在一个尚未执行的函数中。