在SweetAlert2 PreConfirm中传递局部变量-Angular 7

问题描述 投票:0回答:1

我有以下.ts文件

@Component({
  selector: 'app-product-category',
  templateUrl: './product-category.component.html',
  styleUrls: ['./product-category.component.css']
})
export class ProductCategoryComponent implements OnInit {

  constructor(public httpClient: HttpClient) {
  }

  ngOnInit() {   
  }

  delete(name, id) {
    Swal.fire({
      title: 'Are you sure?',
      text: 'Do you want to delete Product Category - ' + name + '?',
      type: 'warning',
      showCancelButton: true,
      showLoaderOnConfirm: true,
      confirmButtonColor: '#3085d6',
      cancelButtonColor: '#d33',
      confirmButtonText: 'Yes, delete it!',
      preConfirm(inputValue: any): Promise<any | void> | any | void {
          // I want to use httpClient to make a http request to the server here
          this.httpClient.delete()...;
      }
    }).then((result) => {
      if (result.value) {
        Swal.fire(
          'Deleted!',
          'Your file has been deleted.',
          'success'
        );
      }
    });
  }
}

我想使用构造函数中定义的httpClientpreConfirm代码块内进行网络调用,但是我无法引用变量(httpClient)。我该怎么办?

angular typescript sweetalert2
1个回答
0
投票

您可以使用箭头功能:

preConfirm: (inputValue: any) => {
  this.httpClient.delete(...)
}
© www.soinside.com 2019 - 2024. All rights reserved.