Angular2无法在promise中访问“this”
发布时间:2020-12-17 18:02:39 所属栏目:安全 来源:网络整理
导读:我无法调用ng2-sweetalert2插件的承诺内的函数 swal({ title: 'Are you sure?',text: "You won't be able to revert this!",type: 'warning',showCancelButton: true,confirmButtonText: 'Yes,delete it!'}).then(function(x) { this.removeNote(key); swal(
我无法调用ng2-sweetalert2插件的承诺内的函数
swal({ title: 'Are you sure?',text: "You won't be able to revert this!",type: 'warning',showCancelButton: true,confirmButtonText: 'Yes,delete it!' }).then(function(x) { this.removeNote(key); swal( 'Deleted!','Your file has been deleted.','success' ); },function(e){ console.log('Cancelled'); }); removeNote(key){ this.todo.remove(key); this.afService.closeNote(key); } this.removeNote()导致错误. EXCEPTION: Uncaught (in promise): TypeError: Cannot read property 'removeNote' of undefined 我该如何克服这个问题?我使用了NgZone,但我得到了同样的错误 解决方法
假设你正在使用TypeScript,你可以使用
arrow function expression,它保留了它的值.
swal({...}).then((x) => console.log(this)); // now 'this' is your component (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |