我有一个错误:
“数字”类型不能分配给“字符串”类型。
此位置有错误:
swal.getContent().querySelector('strong').textContent = swal.getTimerLeft()
我该如何解决?
完整代码:
let timerInterval
swal({
title: 'Auto close alert!',
html: 'I will close in <strong></strong> seconds.',
timer: 2000,
onOpen: () => {
swal.showLoading()
timerInterval = setInterval(() => {
swal.getContent().querySelector('strong')
.textContent = swal.getTimerLeft()
}, 100)
},
onClose: () => {
clearInterval(timerInterval)
}
}).then((result) => {
if (
// Read more about handling dismissals
result.dismiss === swal.DismissReason.timer
) {
console.log('I was closed by the timer')
}
})
答案 0 :(得分:3)
Node.textContent
是字符串,而swal.getTimerLeft()
不返回字符串。
要解决此问题,请使用Number.prototype.toString()
:
swal.getContent().querySelector('strong').textContent = swal.getTimerLeft().toString();