Vue3+Hooks實(shí)現(xiàn)4位隨機(jī)數(shù)和60秒倒計(jì)時(shí)的示例代碼
前言
首先,Vue3的Hooks是一種新的 API,用于在Vue組件中管理狀態(tài)和生命周期。它們是基于 React Hooks的概念設(shè)計(jì)的,可以讓開發(fā)者更方便地在函數(shù)式組件中使用狀態(tài)管理和生命周期函數(shù)。本文給大家分享一下基于Hooks實(shí)現(xiàn)的兩個(gè)案例。
一、示例代碼
(1)/src/views/HelloWorld.vue
<template> <div class="index"> <h1>{{ code }}</h1> <p> <el-input size="small" style="font-size: 13px" placeholder="請(qǐng)輸入驗(yàn)證碼" /> <el-button size="small" :disabled="isDisabled" @click="sendCode">{{ countdownText }}</el-button> </p> <p>{{ `disabled=${isDisabled}` }}</p> </div> </template> <script lang='ts' setup> import { useRandomCode } from '@/hooks/useRandomCode' const code = useRandomCode() import useCountDown from '@/hooks/useCountDown' const { countdownText, isDisabled, sendCode } = useCountDown(60) </script> <style lang="less" scoped> .index { position: relative; display: flex; flex-direction: column; width: 100%; height: 100%; overflow: hidden; justify-content: center; align-items: center; p { display: flex; flex-direction: row; align-items: center; } } </style>
(2)/src/hooks/useRandomCode.js
import { ref, onMounted, onBeforeUnmount, onUnmounted } from 'vue' /** * 4位隨機(jī)數(shù) */ export const useRandomCode = () => { const code = ref(('000' + Math.floor(Math.random() * 10000)).slice(-4)) const fn = () => { let num = Math.floor(Math.random() * 10000) num = ('000' + num).slice(-4) return num } setInterval(() => { code.value = fn() }, 1000) onMounted(() => { console.log('useRandomCode -> onMounted') }) onBeforeUnmount(() => { console.log('useRandomCode -> onBeforeUnmount') }) onUnmounted(() => { console.log('useRandomCode -> onUnmounted') }) return code }
(3)/src/hooks/useCountDown.ts
import { ref, Ref, onMounted, onBeforeUnmount, onUnmounted } from 'vue' /** * 60秒倒計(jì)時(shí) */ export default (num: number, text = '發(fā)送驗(yàn)證碼'): ({ countdownText: Ref<string>, isDisabled: Ref<boolean>, sendCode: () => void }) => { const countdownNum = ref(num) const countdownText = ref(text) const isDisabled = ref(false) const sendCode = () => { countdownText.value = `${countdownNum.value}s` isDisabled.value = true const timer = setInterval(() => { countdownNum.value-- countdownText.value = `${countdownNum.value}s` if (countdownNum.value === 0) { clearInterval(timer) countdownNum.value = num countdownText.value = text isDisabled.value = false } }, 1000) } onMounted(() => { console.log('useCountDown -> onMounted') }) onBeforeUnmount(() => { console.log('useCountDown -> onBeforeUnmount') }) onUnmounted(() => { console.log('useCountDown -> onUnmounted') }) return { countdownText, isDisabled, sendCode } }
二、運(yùn)行效果
三、更多了解見以下文章
詳解hooks在vue3中的使用方法及示例_vue.js_腳本之家
最前端|一文詳解Vue3.x 中 hooks 函數(shù)封裝和使用 - 知乎
到此這篇關(guān)于Vue3+Hooks實(shí)現(xiàn)4位隨機(jī)數(shù)和60秒倒計(jì)時(shí)的示例代碼的文章就介紹到這了,更多相關(guān)Vue3+Hooks 4位隨機(jī)數(shù)和60秒倒計(jì)時(shí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue3實(shí)現(xiàn)獲取驗(yàn)證碼按鈕倒計(jì)時(shí)效果
- Vue3動(dòng)態(tài)倒計(jì)時(shí)的代碼實(shí)現(xiàn)
- vue3實(shí)現(xiàn)封裝時(shí)間計(jì)算-日期倒計(jì)時(shí)組件-還有XX天&第XX天
- 基于Vue3創(chuàng)建一個(gè)簡(jiǎn)單的倒計(jì)時(shí)組件
- vue3發(fā)送驗(yàn)證碼倒計(jì)時(shí)功能的實(shí)現(xiàn)(防止連點(diǎn)、封裝復(fù)用)
- Vue3?實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能
- Vue3?實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能(刷新保持狀態(tài))
- 使用Vue3實(shí)現(xiàn)倒計(jì)時(shí)器及倒計(jì)時(shí)任務(wù)的完整代碼
相關(guān)文章
如何使用sm4js進(jìn)行加密和國(guó)密sm4總結(jié)
近期由于公司項(xiàng)目的需要開始研究國(guó)密SM4加密,下面這篇文章主要給大家介紹了關(guān)于如何使用sm4js進(jìn)行加密和國(guó)密sm4的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04Vue結(jié)合leaflet實(shí)現(xiàn)克里金插值
本文主要介紹了Vue結(jié)合leaflet實(shí)現(xiàn)克里金插值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Vue實(shí)現(xiàn)拖放排序功能的實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家介紹了Vue中實(shí)現(xiàn)拖放排序功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07Vue組件實(shí)現(xiàn)旋轉(zhuǎn)木馬動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Vue組件實(shí)現(xiàn)旋轉(zhuǎn)木馬動(dòng)畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07Vue3?路由頁(yè)面切換動(dòng)畫?animate.css效果
這篇文章主要介紹了Vue3路由頁(yè)面切換動(dòng)畫animate.css效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09vue如何統(tǒng)一樣式(reset.css與border.css)
這篇文章主要介紹了vue如何統(tǒng)一樣式(reset.css與border.css),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05