如何调用方法 i sweetalert 2
How to call method i sweetalert 2
在我的 angular 4 项目中,我正在使用带有 sweetalert 2 的主题,我想在用户单击确认按钮时调用我的方法,但似乎我无法调用其中的任何方法函数
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()
})
我知道这不是 angular sweetalert 但是否可以不做任何更改就让它工作?
使用箭头函数表示法() =>
。当您使用 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
})
正确答案也对我有帮助,尽管我必须解决如何捕获“Uncaught in promise”错误。如果有人发现使用 cancelButton 有帮助,我想我会把它添加到这里完成。
swal({
//code
}).then(()=> {
//code
},(error: any) => console.log(error));
在我的 angular 4 项目中,我正在使用带有 sweetalert 2 的主题,我想在用户单击确认按钮时调用我的方法,但似乎我无法调用其中的任何方法函数
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()
})
我知道这不是 angular sweetalert 但是否可以不做任何更改就让它工作?
使用箭头函数表示法() =>
。当您使用 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
})
正确答案也对我有帮助,尽管我必须解决如何捕获“Uncaught in promise”错误。如果有人发现使用 cancelButton 有帮助,我想我会把它添加到这里完成。
swal({
//code
}).then(()=> {
//code
},(error: any) => console.log(error));