Vue利用dayjs封裝實(shí)現(xiàn)時(shí)間實(shí)時(shí)刷新
1、vue2中使用mixins封裝
1.1 封裝
// mixins/formatdate.js import dayjs from 'dayjs' export default { data() { return { currentTime: { timer: null, currentDay: this.formatTime().day, // 星期幾 date: this.formatTime().date, // 年月日 time: this.formatTime().time, // 時(shí)分秒 }, } }, mounted() { this.timer = setInterval(() => { this.updateTime() }, 1000) }, methods: { formatTime(d = 'YYYY.MM.DD', t = 'HH:mm:ss') { let days = ['日', '一', '二', '三', '四', '五', '六'] let date = dayjs(new Date()).format(d) let time = dayjs(new Date()).format(t) let day = `星期${days[dayjs().day()]}` return { date, time, day } }, updateTime() { this.currentTime.currentDay = this.formatTime().day this.currentTime.date = this.formatTime().date this.currentTime.time = this.formatTime().time }, }, beforeDestroy() { clearInterval(this.timer) }, }
1.2 在組件中使用
<span>{{ currentTime.date }}</span> <span>{{ currentTime.currentDay }}</span> <span>{{ currentTime.time }}</span> <script> import formatdate from '@/mixins/formatdate' export default { mixins: [formatdate], } </script>
2、vue3中利用組合式函數(shù)
2.1 封裝
// formatTime.js import dayjs from 'dayjs' import { onBeforeUnmount, onMounted, ref } from 'vue' export function useTime() { // 星期幾 const currentDay = ref('') // 年月日 const date = ref('') // 時(shí)分秒 const time = ref('') // 獲取時(shí)間 const updateTime = (d = 'YYYY.MM.DD', t = 'HH:mm:ss') => { let days = ['日', '一', '二', '三', '四', '五', '六'] date.value = dayjs(new Date()).format(d) time.value = dayjs(new Date()).format(t) currentDay.value = `星期${days[dayjs().day()]}` } // 定義定時(shí)器 let timer = null onMounted(() => { timer = setInterval(() => { updateTime() }, 1000) }) onBeforeUnmount(() => clearInterval(timer)) return { currentDay, date, time } }
2.2 在組件中使用
<span>{{ currentDay }}</span> <span>{{ date }}</span> <span>{{ time }}</span> <script setup> import { useTime } from '@/utils/formatTime' const { currentDay, date, time } = useTime() </script>
到此這篇關(guān)于Vue利用dayjs封裝實(shí)現(xiàn)時(shí)間實(shí)時(shí)刷新的文章就介紹到這了,更多相關(guān)Vue dayjs時(shí)間實(shí)時(shí)刷新內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何使用crypto-js對(duì)文件上傳下載進(jìn)行加密處理
這篇文章主要介紹了如何使用crypto-js對(duì)文件上傳下載進(jìn)行加密處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05詳解Vue學(xué)習(xí)筆記進(jìn)階篇之列表過(guò)渡及其他
本篇文章主要介紹了詳解Vue學(xué)習(xí)筆記進(jìn)階篇之列表過(guò)渡及其他,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07vue+j簡(jiǎn)單的實(shí)現(xiàn)輪播效果,滾動(dòng)公告,銜接
這篇文章主要介紹了vue+j簡(jiǎn)單的實(shí)現(xiàn)輪播效果,滾動(dòng)公告,銜接,文章圍繞主題的相關(guān)資料展開(kāi)詳細(xì)的內(nèi)容具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06vite build vue3項(xiàng)目配置開(kāi)啟sourcemap方式
這篇文章主要介紹了vite build vue3項(xiàng)目配置開(kāi)啟sourcemap方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06Vue使用html2canvas實(shí)現(xiàn)截取圖片并保存
html2canvas是一個(gè)JavaScript庫(kù),它可以將HTML元素轉(zhuǎn)換為Canvas元素本文將介紹一下Vue如何使用html2canvas實(shí)現(xiàn)截取圖片并保存功能,需要的可以參考下2023-12-12前端vue2使用國(guó)密SM4進(jìn)行加密、解密具體方法
SM4是一種對(duì)稱加密算法,類似于AES,主要用于數(shù)據(jù)的批量加密,如文件加密、數(shù)據(jù)庫(kù)加密、網(wǎng)絡(luò)通信數(shù)據(jù)加密等,這篇文章主要給大家介紹了關(guān)于前端vue2使用國(guó)密SM4進(jìn)行加密、解密的相關(guān)資料,需要的朋友可以參考下2024-07-07如何在vue3中使用滑塊檢驗(yàn)vue-puzzle-verification
這篇文章主要介紹了在vue3中使用滑塊檢驗(yàn)vue-puzzle-verification的相關(guān)資料,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11vue項(xiàng)目中使用ts(typescript)入門(mén)教程
最近項(xiàng)目需要將原vue項(xiàng)目結(jié)合ts的使用進(jìn)行改造,本文從安裝到vue組件編寫(xiě)進(jìn)行了說(shuō)明,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11