Vue 3 is the latest version of the easy to use Vue JavaScript framework that lets us create front end apps.
In this article, we’ll look at how to create a digital clock app with Vue 3 and JavaScript.
We can create the Vue project with Vue CLI.
To install it, we run:
npm install -g @vue/cli
with NPM or:
yarn global add @vue/cli
with Yarn.
Then we run:
vue create digital-clock
and select all the default options to create the project.
We can create a digital clock app with the following code:
<template>
<div>{{ dateTime.hours }}:{{ dateTime.minutes }}:{{ dateTime.seconds }}</div>
</template>
<script>
const date = new Date();
export default {
name: "App",
data() {
return {
dateTime: {
hours: date.getHours(),
minutes: date.getMinutes(),
seconds: date.getSeconds(),
},
timer: undefined,
};
},
methods: {
setDateTime() {
const date = new Date();
this.dateTime = {
hours: date.getHours(),
minutes: date.getMinutes(),
seconds: date.getSeconds(),
};
},
},
beforeMount() {
this.timer = setInterval(this.setDateTime, 1000);
},
beforeUnmount() {
clearInterval(this.timer);
},
};
</script>
#javascript