javascript - Sweetalert 中的服务无法运行

标签 javascript jquery angular typescript

我知道以前有人问过这个问题,但我似乎找不到答案,我想询问 sweetalertsweetalert 包含服务中的组件中不起作用用于删除数据。

enter image description here

组件

Swal({
    title: 'Are you sure?',
    text: 'You will not be able to recover this imaginary file!',
    showCancelButton: true,
    confirmButtonText: 'Yes, delete it!',
    cancelButtonText: 'No, keep it'
}).then((result) => {
    if (result.value) {
        this.bookingService.deletedata(data._id).subscribe(data => {
            console.log(data);
        });
        Swal(
            'Deleted!',
            'Your imaginary file has been deleted.',
            'success'
        )
    } else if (result.dismiss === Swal.DismissReason.cancel) {
        Swal(
            'Cancelled',
            'Your imaginary file is safe :)',
            'error'
        )
    }
})

服务

deletedata(param) {
        console.log(param);
        let searchUrl = Urlapi + '/pesan/remove' + param;
        return this.http.delete(searchUrl, this.jwt()).map(res => res.json());
    }

最佳答案

你需要分配 let me = this;在函数开始时。

let me = this;

Swal({
    title: 'Are you sure?',
    text: 'You will not be able to recover this imaginary file!',
    showCancelButton: true,
    confirmButtonText: 'Yes, delete it!',
    cancelButtonText: 'No, keep it'
}).then((result) => {
    if (result.value) {
        me.bookingService.deletedata(data._id).subscribe(data => {
            console.log(data);
        });
        Swal(
            'Deleted!',
            'Your imaginary file has been deleted.',
            'success'
        )
    } else if (result.dismiss === Swal.DismissReason.cancel) {
        Swal(
            'Cancelled',
            'Your imaginary file is safe :)',
            'error'
        )
    }
})

关于javascript - Sweetalert 中的服务无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51796646/

相关文章:

javascript - JS Promise 无法返回结果

javascript:如何将字符串数组转换为连接以逗号分隔的项目的纯字符串?

angular - 避免 RxJs 6 中的嵌套 subscribe() 调用

Angular 2 : What is the exact difference between private and public attributes in components?

javascript - 用户选择选项未使用 jQuery 更新

javascript - 在 jQuery BootGrid 中重新加载不起作用

jquery - 如何在 JQuery Fullcalendar 中使用 slotMinutes

javascript - 根据文本长度自动在表格中显示更多/更少的文本

javascript - 如何用数组值替换字符串中的多个占位符?

javascript - AngularJS : difference between views and templateUrl