【问题标题】:SweetAlert delete errors in angular5SweetAlert 删除 angular5 中的错误
【发布时间】:2018-11-20 23:21:17
【问题描述】:

使用 angular5,我尝试在代码中使用 SweetAlert 删除选项,但出现了一些错误:

我用的是和官方link一样的功能

这是.ts中的代码

import * as _swal from 'sweetalert';
import { SweetAlert } from 'sweetalert/typings/core';
const swal: SweetAlert = _swal as any;

@Component({
  selector: 'app-clients',
  templateUrl: './clients.component.html',
  styleUrls: ['./clients.component.scss']
})
export class ClientsComponent implements OnInit {

  pageClients:any;

   ngOnInit() {
    this.doSearch();
    this.getAllClientsList();
  }

  delete(p:Clients){

    swal({
          title: "Are you sure?",
          text: "Once deleted, you will not be able to recover this imaginary file!",
          icon: "warning",
          buttons: true,
          dangerMode: true,
        })
          .then((willDelete) => {
            if (willDelete) {
                  this.clientService.deleteClient(p.id)
                  .subscribe(data=>{
                  this.pageClients.content.splice(
                  this.pageClients.content.indexOf(p),1
                     );
                  },err=>{
                  console.log(err);
                   })
              swal("Poof! Your imaginary file has been deleted!", {
                icon: "success",
              });
            } else {
              swal("Your imaginary file is safe!");
            }
          });
    }
}

如何解决?

编辑

delete(p:Clients){

    swal({
      title: "are you sur ?",
      text: " nce deleted, you will not be able to recover this imaginary file!!",
      type: 'warning',
      showConfirmButton: true,
      showCancelButton: true
    })
      .then((willDelete) => {

        if (willDelete) {

          this.clientService.deleteClient(p.id)
            .subscribe(data=>{
              this.pageClients.content.splice(
                this.pageClients.content.indexOf(p),1
              );
            },err=>{
              console.log(err);
            })

          swal({
           title: " great",
            type: 'success',
          });
        } else {
          swal("you have not deleted this client yet ");
        }
      });
  }

当此警报出现时,无论何时我选择“确定”或“取消”,我都会收到“很棒”的警报。

当我试图打印这个时,我明白它总是在执行第一个 if(willDelete),所以无论我选择 ok 还是 cancel,这个客户端都会被删除。

【问题讨论】:

    标签: angular angular5 frontend sweetalert


    【解决方案1】:

    当您尝试使用swal 函数时,您只需提供SweetAlertOptions 上可用的属性接口它有很多属性,但这些属性不存在

    icon: "warning",
     buttons: true,
     dangerMode: true,
    

    您可以改为使用:

    type :'warning',
    showConfirmButton: true,
    showCancelButton: true,
    

    最终代码

    swal({
      title: "Are you sure?",
      text: "Once deleted, you will not be able to recover this imaginary file!",
      type: 'warning',
      showConfirmButton: true,
      showCancelButton: true     
    })
    .then((willDelete) => {
      if (willDelete) {
            this.clientService.deleteClient(p.id)
            .subscribe(data=>{
            this.pageClients.content.splice(
            this.pageClients.content.indexOf(p),1
               );
            },err=>{
            console.log(err);
             })
        swal({title:"Poof! Your imaginary file has been deleted!",
          type: "success",
        });
      } else {
        swal("Your imaginary file is safe!");
      }
    });
    

    只是为了确定

    我正在使用以下 src ngx-sweetalert2

    npm install --save sweetalert2 @toverux/ngx-sweetalert2
    

    Stackblitz demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多