首页 > 解决方案 > 如何在创建计时器中使用 setInterval

问题描述

我在这样的页面中有一个计时器。

<h5 class="pa-2 text-center">{{ timestamp }}</h5>

初始timestamp: '',数据,我有一个设置时间戳的功能。

方法中的功能

getNow() {
  const today = new Date();
  const date1 = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
  const time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
  const dateTime = date1 + "    " + time;
  this.timestamp = dateTime;
}

并在mounted中使用此功能。

setInterval(() => this.getNow() , 1000);

我每秒有 2 个错误。错误: [Vue 警告]:属性或方法“日期”未在实例上定义,但在渲染期间被引用。通过初始化该属性,确保该属性是反应性的,无论是在数据选项中,还是对于基于类的组件。

标签: nuxt.js

解决方案


推荐阅读