import { ref, onMounted, onUnmounted } from "vue";
时间: 2024-05-20 18:16:25 浏览: 17
// This code is written in Vue.js 3 syntax.
// Define a reactive variable to store the current time.
const currentTime = ref(new Date());
// Define a function to update the current time every second.
function updateTime() {
currentTime.value = new Date();
}
// Call the updateTime function every second using the setInterval method.
// Save the interval ID so that it can be cleared when the component is unmounted.
let intervalId = null;
onMounted(() => {
intervalId = setInterval(updateTime, 1000);
});
// Clear the interval when the component is unmounted to prevent memory leaks.
onUnmounted(() => {
clearInterval(intervalId);
});
// Export the currentTime variable so that it can be used in other components.
export default {
currentTime,
};