如何调用方法I Sweetalert 2

Ale*_*dro 1 sweetalert angular sweetalert2

在我的angular 4项目中,我正在使用带有Sweetalert 2的主题,我想在用户单击确认按钮时调用我的方法,但是看来我无法在then函数中调用任何方法

swal({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!'
}).then(function () {

  // I want to call my method here but I can't do this.myMethod()

})
Run Code Online (Sandbox Code Playgroud)

我知道这不是棱角分明的甜言蜜语,但有可能使其无需更改就能正常工作吗?

Fai*_*sal 5

使用箭头功能符号() =>。使用function关键字时,您将失去对的访问this。将代码更改为以下代码:

swal({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!'
}).then(()=> {

  this.myMethod(); // this should execute now

})
Run Code Online (Sandbox Code Playgroud)