【发布时间】:2020-09-30 18:56:41
【问题描述】:
你好,所以我在 Vue 中制作了一个基本时钟,出于某种原因,我使用此代码将初始时间值输入到 bazinga,但一旦更新它就表明它是未定义的。如果我也在 setInterval() 函数中定义 now 和其他变量,我设法“让它工作”,但这似乎不是一个好的选择。 this.bazinga 怎么会在第一次迭代后变得未定义?
<template>
<div id="clock">
<h2>The time is now</h2>
<p>{{ bazinga }}</p>
</div>
</template>
<style>
</style>
<script>
export default {
data(){
const now = new Date()
const hours = now.getHours()
const minutes = now.getMinutes()
const seconds = now.getSeconds()
return{
bazinga: now
}
},
created() {
setInterval(() => {
console.log(this.bazinga)
this.bazinga = this.now
console.log(this.bazinga)
}, 1000)
},
destroyed() {
clearInterval(this.now)
}
}
</script>
【问题讨论】:
-
this.now未定义,因为您没有在data或computed属性中包含此属性,这就是为什么this.bazinga = this.now将使this.bazinga未定义。
标签: vue.js