Hai,
Saya mendapatkan kesalahan "Jenis 'tombol' properti tidak kompatibel" di komponen saya. Ini kode dan kesalahannya:
`delete(id, $index) {
swal({
title: "Are you sure?",
text: "Are you sure to delete the menu? It cannot be undone.",
icon: "warning",
dangerMode: true,
buttons: {
cancel: "Cancel",
ok: "OK"
}
})
.then(willDelete => {
if (willDelete) {
Helpers.setLoading(true);
this.service
.delete({ _id: id})
.subscribe(data => {
if (data.type) {
this.cars.splice($index, 1);
Helpers.setLoading(false);
}
});
}
});
}`
aku mengerti
Argument of type '{ title: string; text: string; icon: string; dangerMode: true; buttons: { cancel: string; ok: str...' is not assignableto parameter of type 'string | Partial<SwalOptions>'.
Type '{ title: string; text: string; icon: string; dangerMode: true; buttons: { cancel: string; ok: str...' is not assignable to type 'Partial<SwalOptions>'.
Types of property 'buttons' are incompatible.
Type '{ cancel: string; ok: string; }' is not assignable to type 'ButtonList'.
Property 'cancel' is incompatible with index signature.
Type 'string' is not assignable to type 'ButtonOptions'.
Apa yang bisa menjadi alasan?
buttons: [true, true]
akan memberikan default cancel
dan ok
buttons: ['Cancel', 'Ok']
akan memberikan tombol bernama Cancel
dan Ok
Kesalahan menjadi seperti:
error TS2345: Argument of type '{ title: string; text: string; icon: string; dangerMode: true; buttons: boolean[]; }' is not assignable to parameter oftype 'string | Partial<SwalOptions>'.
Type '{ title: string; text: string; icon: string; dangerMode: true; buttons: boolean[]; }' is not assignable to type 'Partial<SwalOptions>'.
Types of property 'buttons' are incompatible.
Type 'boolean[]' is not assignable to type 'ButtonList'.
Index signature is missing in type 'boolean[]'.
Anda menghadapi kesalahan TypeScript, pastikan untuk mengikuti jenis ini
export interface ButtonOptions {
visible: boolean,
text: string,
value: any,
className: string,
closeModal: boolean,
};
export interface ButtonList {
[buttonNamespace: string]: ButtonOptions,
};
Tampaknya buttons:
tidak dapat berupa array boolean, tetapi harus berupa ButtonList.
Anda dapat mencoba
buttons: {
cancel: { text: 'Cancel' },
confirm: { text: 'Confirm' },
}
Mengapa saya harus menambahkan kode ini? Saya tidak bisa mendapatkan apa yang harus saya lakukan
Mengapa opsi tidak opsional dalam TypeScript? Anda harus melewati semua nilai alih-alih yang Anda butuhkan. jQuery misalnya suka:
interface DialogOptions {
closeBtn?: string;
closeBtnText?: string;
corners?: boolean;
initSelector?: string;
overlayTheme?: string;
}
@ splitt3r Anda benar, itu pasti opsional. Jika ada yang ingin mengirim permintaan tarik untuk memperbaikinya, silakan!
### This will work
swal("you liked it",{
buttons:{cancel:false,confirm:false},
timer:1000,
})
swal({
title: "Are you sure?",
text: "Are you sure to delete the menu? It cannot be undone.",
icon: "warning",
dangerMode: true,
buttons: {
cancel: "Cancel",
ok: "OK"
}
} as any)
Terima kasih, saya bekerja dengan Angular 10 , Swal.fire({
judul: 'Espere',
teks: 'Informasi Penjaga',
ty: 'info',
allowOutsideClick: false
}sebagai apapun);
Swal.showLoading();
Komentar yang paling membantu
buttons: [true, true]
akan memberikan defaultcancel
danok
buttons: ['Cancel', 'Ok']
akan memberikan tombol bernamaCancel
danOk