在 SweetAlert2 PreConfirm 中传递局部变量 - Angular 7
Pass Local Variable in SweetAlert2 PreConfirm - Angular 7
我有以下 .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)。我该怎么做?
可以使用箭头函数:
preConfirm: (inputValue: any) => {
this.httpClient.delete(...)
}
我有以下 .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)。我该怎么做?
可以使用箭头函数:
preConfirm: (inputValue: any) => {
this.httpClient.delete(...)
}