类型 'number' 不可分配给类型 'string'
Type 'number' is not assignable to type 'string'
我有这样一个错误:
Type 'number' is not assignable to type 'string'.
此处错误:
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')
}
})
Node.textContent
是一个字符串,swal.getTimerLeft()
不是 return 一个字符串。
要解决此问题,请使用 Number.prototype.toString()
:
Swal.getHtmlContainer().querySelector('strong').textContent = swal.getTimerLeft().toString();
我有这样一个错误:
Type 'number' is not assignable to type 'string'.
此处错误:
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')
}
})
Node.textContent
是一个字符串,swal.getTimerLeft()
不是 return 一个字符串。
要解决此问题,请使用 Number.prototype.toString()
:
Swal.getHtmlContainer().querySelector('strong').textContent = swal.getTimerLeft().toString();