参见英文答案 > setTimeout and “this” in JavaScript 5个
> How to access the correct `this` inside a callback? 10个
所以我在我的应用程序中使用了uikit confirmation modal.我的问题是,当我要点击< button>确认.这个内部函数是未定义的.这是我的代码……
declare var UIkit:any;
deleteData(dataArr): void {
UIkit.modal.confirm('Are you sure you want to delete this?', function() {
console.log(dataArr);
console.log(this);
//use service here...
UIkit.modal.alert('Confirmed!');
});
}
在该函数内部我希望使用http请求服务,但我遇到了这个问题.我正在使用Angular 2.x.
解决方法:
使用箭头功能……
declare var UIkit:any;
deleteData(dataArr): void {
UIkit.modal.confirm('Are you sure you want to delete this?', () => {
console.log(this);
// [...]
});
}
有关此事的详细信息,请查看MDN: Arrow functions.
An arrow function does not create its own this context, so
this
has
its original meaning from the enclosing context.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。