//BaseTime.vue
<template>
<div>{{ now }}</div>
</template>
<script>
import moment from "moment";
export default {
data: function() {
this.moment = moment;
return {
now: moment(new Date()).format("YYYY-MM-DD HH:mm:ss"),
};
},
mounted() {
this.startClock();
},
beforeDestroy() {
clearInterval(this.clockInterval);
},
methods: {
startClock() {
clearInterval(this.clockInterval);
this.clockInterval = setInterval(() => {
this.now = moment(new Date()).format("YYYY-MM-DD HH:mm:ss");
}, 1000);
}
}
};
</script>