Javascript vuejs当前时间戳移动

Javascript vuejs当前时间戳移动,javascript,typescript,vue.js,vuejs2,Javascript,Typescript,Vue.js,Vuejs2,我有当前的时间戳功能,返回日期和时间,但它不移动(意思是:秒和分钟等,只显示页面加载和不活动的时间) 我想要的:我想要时间移动,而不是静止 代码 脚本 methods: { currentDateTime() { const current = new Date(); const date = current.getFullYear() + "-" + (current.getMonth() + 1)

我有当前的时间戳功能,返回日期和时间,但它不移动(意思是:秒和分钟等,只显示页面加载和不活动的时间)

我想要的:我想要时间移动,而不是静止

代码
脚本

methods: {
  currentDateTime() {
      const current = new Date();
      const date =
        current.getFullYear() +
        "-" +
        (current.getMonth() + 1) +
        "-" +
        current.getDate();
      const time =
        current.getHours() +
        ":" +
        current.getMinutes() +
        ":" +
        current.getSeconds();
      const dateTime = date + " " + time;

      return dateTime;
    },
}
HTML

{{ currentDateTime() }}
{{ timestamp }}
屏幕截图

有什么想法吗?
currentDateTime()
只执行一次。使用
setInterval()
每1秒执行一次

data: {
    timestamp: ''
},
mounted: function () {
    setInterval(() => { this.currentDateTime() }, 1000)
  }
}),
methods: {
  currentDateTime() {
      const current = new Date();
      const date =
        current.getFullYear() +
        "-" +
        (current.getMonth() + 1) +
        "-" +
        current.getDate();
      const time =
        current.getHours() +
        ":" +
        current.getMinutes() +
        ":" +
        current.getSeconds();
      const dateTime = date + " " + time;
      this.timestamp = dateTime;
    },
}
HTML

{{ currentDateTime() }}
{{ timestamp }}
currentDateTime()
将只执行一次。使用
setInterval()
每1秒执行一次

data: {
    timestamp: ''
},
mounted: function () {
    setInterval(() => { this.currentDateTime() }, 1000)
  }
}),
methods: {
  currentDateTime() {
      const current = new Date();
      const date =
        current.getFullYear() +
        "-" +
        (current.getMonth() + 1) +
        "-" +
        current.getDate();
      const time =
        current.getHours() +
        ":" +
        current.getMinutes() +
        ":" +
        current.getSeconds();
      const dateTime = date + " " + time;
      this.timestamp = dateTime;
    },
}
HTML

{{ currentDateTime() }}
{{ timestamp }}

不要忘记在
销毁前
清除间隔
。不要忘记在
销毁前
清除间隔