在这里插入代码片
const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
return [year, ' 年 ', month, ' 月 ', day, ' 日 '].map(formatNumber).join('')
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
module.exports = {
formatTime: formatTime,
}
<div class="weather">{{ timer }},多云转晴</div>
data() {
return {
timer: '',
}
mounted() {
this.timer = setInterval(() => {
this.timer = util.formatTime(new Date())
}, 1000)
}
beforeDestroy() {
if (this.timer) {
clearInterval(this.timer)
}
},