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)文章
vue2項(xiàng)目中封裝echarts地圖的優(yōu)雅方法
這篇文章主要給大家介紹了關(guān)于vue2項(xiàng)目中封裝echarts地圖的優(yōu)雅方法,需要的朋友可以參考下2022-03-03
vue 的 solt 子組件過(guò)濾過(guò)程解析
這篇文章主要介紹了vue 的 solt 子組件過(guò)濾過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
vue項(xiàng)目打包自動(dòng)更新版本號(hào)且自動(dòng)刷新緩存的方法示例
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目打包自動(dòng)更新版本號(hào)且自動(dòng)刷新緩存的相關(guān)資料,文中通過(guò)代碼及圖文介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-11-11
vue項(xiàng)目拍照或上傳圖片并實(shí)現(xiàn)轉(zhuǎn)化為base64格式
這篇文章主要介紹了vue項(xiàng)目拍照或上傳圖片并實(shí)現(xiàn)轉(zhuǎn)化為base64格式方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Vue實(shí)現(xiàn)跑馬燈樣式文字橫向滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)跑馬燈樣式文字橫向滾動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
vue2.x 對(duì)象劫持的原理實(shí)現(xiàn)
這篇文章主要介紹了vue2.x 對(duì)象劫持的原理實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Vue3和i18n實(shí)現(xiàn)多語(yǔ)言方式
這篇文章主要介紹了Vue3和i18n實(shí)現(xiàn)多語(yǔ)言方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
初識(shí)簡(jiǎn)單卻不失優(yōu)雅的Vue.js
這篇文章主要為大家介紹了簡(jiǎn)單卻不失優(yōu)雅、小巧而不乏大匠的Vue.js,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

