Vue3前端項(xiàng)目實(shí)現(xiàn)動(dòng)態(tài)顯示當(dāng)前系統(tǒng)時(shí)間詳解
前言
在 Vue3 項(xiàng)目中,動(dòng)態(tài)顯示當(dāng)前系統(tǒng)時(shí)間是一個(gè)常見的需求,例如在數(shù)據(jù)看板、儀表盤或后臺(tái)管理系統(tǒng)中展示“截止時(shí)間”或“當(dāng)前時(shí)間”。本文將詳細(xì)介紹如何使用 Vue3 的 ref 和生命周期鉤子實(shí)現(xiàn)一個(gè)高效、可維護(hù)的動(dòng)態(tài)時(shí)間顯示組件。
一、實(shí)現(xiàn)思路
1. 核心功能需求
- 實(shí)時(shí)更新:時(shí)間應(yīng)隨系統(tǒng)時(shí)間自動(dòng)更新(如每分鐘刷新一次)。
- 格式化顯示:時(shí)間需格式化為
YYYY年MM月DD日 HH:MM:SS的形式,并確保個(gè)位數(shù)補(bǔ)零(如08:05:09)。 - 性能優(yōu)化:避免頻繁更新(如每秒更新)導(dǎo)致不必要的渲染開銷。
2. 技術(shù)選型
- Vue3 Composition API:使用
ref管理響應(yīng)式數(shù)據(jù),onMounted處理初始化邏輯。 - JavaScript Date 對(duì)象:獲取系統(tǒng)時(shí)間并格式化。
- 定時(shí)器(setInterval):控制更新頻率。
二、代碼實(shí)現(xiàn)
1. 模板部分(Template)
在模板中綁定動(dòng)態(tài)時(shí)間數(shù)據(jù),并通過 CSS 調(diào)整樣式(如位置、顏色):
<template>
<div class="app-container">
<h1 class="h2size" style="top: 0%; left: 3%; color: #d1d8e0">
截止時(shí)間:{{ currentDate }}
</h1>
</div>
</template>
<style scoped>
/* 文字樣式 */
.h2size {
color: rgb(166, 202, 244);
position: absolute;
font-size: 1vw;
font-family: 'Arial', sans-serif;
}
</style>
2. 腳本部分(Script)
使用 Vue3 的 setup 語法糖,結(jié)合 ref 和生命周期鉤子實(shí)現(xiàn)邏輯:
<script setup>
import { ref, onMounted } from "vue";
// 響應(yīng)式日期變量
const currentDate = ref('');
// 格式化日期函數(shù)
function formatDateshort(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // 補(bǔ)零
const day = String(date.getDate()).padStart(2, '0');
const hour = String(date.getHours()).padStart(2, '0');
const minute = String(date.getMinutes()).padStart(2, '0');
const second = String(date.getSeconds()).padStart(2, '0');
return `${year}年${month}月${day}日 ${hour}:${minute}:${second}`;
}
// 組件掛載時(shí)初始化時(shí)間并設(shè)置定時(shí)器
onMounted(() => {
// 初始時(shí)間
currentDate.value = formatDateshort(new Date());
// 每分鐘更新一次時(shí)間
setInterval(() => {
currentDate.value = formatDateshort(new Date());
}, 60000); // 60秒更新一次
});
</script>
三、關(guān)鍵點(diǎn)解析
1. 時(shí)間格式化
問題:直接使用 date.getHours() 等方法返回的是數(shù)字(如 5),需補(bǔ)零為 05。
解決方案:通過 String().padStart(2, '0') 實(shí)現(xiàn)個(gè)位數(shù)補(bǔ)零:
const minute = String(date.getMinutes()).padStart(2, '0');
2. 性能優(yōu)化
避免每秒更新:使用 setInterval 每分鐘(60000ms)更新一次,減少渲染壓力。
清理定時(shí)器:在組件卸載時(shí)(onBeforeUnmount)清除定時(shí)器(本文未展示,但實(shí)際項(xiàng)目中建議添加)。
3. 響應(yīng)式數(shù)據(jù)
ref 的使用:currentDate 是響應(yīng)式變量,模板會(huì)自動(dòng)更新當(dāng)值變化時(shí)。
四、擴(kuò)展功能
1. 自定義更新頻率
可通過 props 傳入更新間隔(如每秒/每分鐘):
const props = defineProps({
updateInterval: { type: Number, default: 60000 }
});
onMounted(() => {
setInterval(() => {
currentDate.value = formatDateshort(new Date());
}, props.updateInterval);
});
2. 國際化支持
修改 formatDateshort 函數(shù),支持多語言格式:
function formatDateshort(date, locale = 'zh-CN') {
return date.toLocaleString(locale, {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
});
}
五、完整代碼示例
<template>
<div class="app-container">
<h1 class="h2size">截止時(shí)間:{{ currentDate }}</h1>
</div>
</template>
<script setup>
import { ref, onMounted, onBeforeUnmount } from "vue";
const currentDate = ref('');
let timer = null;
function formatDateshort(date) {
const pad = num => String(num).padStart(2, '0');
return `${date.getFullYear()}年${pad(date.getMonth() + 1)}月${pad(date.getDate())}日 ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
}
onMounted(() => {
currentDate.value = formatDateshort(new Date());
timer = setInterval(() => {
currentDate.value = formatDateshort(new Date());
}, 60000);
});
onBeforeUnmount(() => {
clearInterval(timer); // 清理定時(shí)器
});
</script>
<style scoped>
.h2size {
color: rgb(166, 202, 244);
position: absolute;
font-size: 1vw;
font-family: 'Arial', sans-serif;
}
</style>
六、效果

總結(jié)
通過 Vue3 的響應(yīng)式數(shù)據(jù)和生命周期鉤子,可以輕松實(shí)現(xiàn)動(dòng)態(tài)時(shí)間顯示。關(guān)鍵點(diǎn)包括:
- 使用
ref管理動(dòng)態(tài)數(shù)據(jù)。 - 通過
padStart補(bǔ)零格式化時(shí)間。 - 合理設(shè)置更新頻率(如每分鐘)平衡實(shí)時(shí)性和性能。
實(shí)際項(xiàng)目中,可進(jìn)一步擴(kuò)展為可配置的組件,支持多語言或自定義格式。
到此這篇關(guān)于Vue3前端項(xiàng)目實(shí)現(xiàn)動(dòng)態(tài)顯示當(dāng)前系統(tǒng)時(shí)間詳解的文章就介紹到這了,更多相關(guān)Vue3動(dòng)態(tài)顯示系統(tǒng)時(shí)間內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue中設(shè)置el-select的高度不生效問題及解決方案
文章介紹了如何使用ElementUI框架中的el-select組件,并解決設(shè)置其高度不生效的問題,在Vue2.x中,使用/deep/關(guān)鍵字可以穿透組件作用域修改樣式;在Vue2.6+到Vue3初期,推薦使用::v-deep關(guān)鍵字;在最新的Vue3和構(gòu)建工具中,推薦使用:deep()偽類來代替::v-deep2025-01-01
Element-Plus表格實(shí)現(xiàn)Table自定義合并行數(shù)據(jù)
在開發(fā)項(xiàng)目中,我們時(shí)常會(huì)用到表格,許多需求可能會(huì)要求自定義特定的行或列,下面就跟隨小編一起來學(xué)習(xí)一下在實(shí)際開發(fā)中如何實(shí)現(xiàn)這一要求吧2024-12-12
基于vue+openlayer實(shí)現(xiàn)地圖聚合和撒點(diǎn)效果
Openlayers 是一個(gè)模塊化、高性能并且功能豐富的WebGIS客戶端的JavaScript包,用于顯示地圖及空間數(shù)據(jù),并與之進(jìn)行交互,具有靈活的擴(kuò)展機(jī)制,本文給大家介紹vue+openlayer實(shí)現(xiàn)地圖聚合效果和撒點(diǎn)效果,感興趣的朋友一起看看吧2021-09-09
項(xiàng)目遷移vite引入圖片資源報(bào)require?is?not?defined問題的解決辦法
這篇文章主要給大家介紹了關(guān)于項(xiàng)目遷移vite引入圖片資源報(bào)require?is?not?defined問題的解決辦法,文中通過代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vite具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01
Vue+tracking.js 實(shí)現(xiàn)前端人臉檢測(cè)功能
這篇文章主要介紹了Vue+tracking.js 實(shí)現(xiàn)前端人臉檢測(cè)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
vue實(shí)現(xiàn)頁面上傳文件夾壓縮后傳給服務(wù)器的操作
這篇文章主要介紹了vue實(shí)現(xiàn)頁面上傳文件夾壓縮后傳給服務(wù)器的操作,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
Vue.js中使用Vuex實(shí)現(xiàn)組件數(shù)據(jù)共享案例
這篇文章主要介紹了Vue.js中使用Vuex實(shí)現(xiàn)組件數(shù)據(jù)共享案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
基于vue.js實(shí)現(xiàn)側(cè)邊菜單欄
這篇文章主要為大家詳細(xì)介紹了基于vue.js實(shí)現(xiàn)側(cè)邊菜單欄的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03

