Передать локальную переменную в SweetAlert2 PreConfirm - Angular 7 - PullRequest
0 голосов
/ 08 ноября 2019

У меня есть следующий .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'
        );
      }
    });
  }
}

Я хочу использовать httpClient, определенный в конструкторе, для сетевого вызова внутри блока кода preConfirm, но я не могу сослатьсяпеременная (httpClient). Как мне это сделать?

1 Ответ

1 голос
/ 08 ноября 2019

Вы можете использовать функцию стрелки:

preConfirm: (inputValue: any) => {
  this.httpClient.delete(...)
}
...