【1】在一个dom元素里面放一个要显示的时间
<div class="updatetime">更新时间:{{ nowDate }}</div>
【2】在data里面定义一个nowDate和一个定时器
data() {
return {
nowDate: null,
nowtimer: "",
}
【3】在methods里面写一个获取当前时间的方法:
methods: {
gettime() {
this.nowDate = new Date().toLocaleString();
},}
【4】在created生命周期里面放一个定时器,定时读取获取当前时间的方法,每秒触发一次
created() {
this.nowtimer = setInterval(this.gettime, 1000);
},