CountDown 倒计时组件
用于实现倒计时功能的组件,常用于验证码倒计时、活动倒计时等场景。
CountButton 倒计时按钮
基本用法
<template>
<CountButton
:value="resetKey"
:before-start-func="sendCode"
:count="60"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { CountButton } from '@/components/CountDown';
const resetKey = ref(0);
async function sendCode() {
// 发送验证码的逻辑
console.log('发送验证码');
return true;
}
</script>Props 属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| value | object | number | string | any[] | - | 控制状态,值变化时重置倒计时(设为 undefined 时重置) |
| count | number | 60 | 倒计时秒数 |
| beforeStartFunc | () => Promise<boolean> | - | 开始前的回调函数,返回 true 开始倒计时,返回 false 或异常不开始 |
继承 Ant Design Vue 的 Button 组件,支持所有 Button 属性(如 type、size、danger 等)。
按钮状态说明
按钮有三种状态:
- 正常状态 - 显示 "获取验证码"(来自
component.countdown.normalText国际化配置) - 倒计时中 - 显示 "{n} 秒后重试"(来自
component.countdown.sendText国际化配置),按钮禁用 - 加载中 - 当
beforeStartFunc执行时显示 loading 状态
验证码倒计时
<template>
<a-space>
<a-input v-model:value="phone" placeholder="请输入手机号" style="width: 200px;" />
<CountButton
:value="resetKey"
:before-start-func="sendCode"
:count="60"
/>
</a-space>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { CountButton } from '@/components/CountDown';
import { message } from 'ant-design-vue';
const phone = ref('');
const resetKey = ref(0);
async function sendCode() {
if (!phone.value) {
message.error('请输入手机号');
return false;
}
// 发送验证码
console.log('发送验证码到:', phone.value);
// await sendSmsCode(phone.value);
return true; // 返回 true 开始倒计时
}
</script>重置倒计时
<template>
<a-space>
<CountButton
:value="resetKey"
:before-start-func="sendCode"
:count="60"
/>
<a-button @click="resetCountdown">重置倒计时</a-button>
</a-space>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { CountButton } from '@/components/CountDown';
const resetKey = ref(0);
async function sendCode() {
console.log('发送验证码');
return true;
}
function resetCountdown() {
// 通过改变 value 值来重置倒计时
resetKey.value++;
}
</script>自定义按钮样式
<template>
<CountButton
:value="resetKey"
:before-start-func="sendCode"
:count="60"
type="primary"
size="large"
danger
/>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { CountButton } from '@/components/CountDown';
const resetKey = ref(0);
async function sendCode() {
return true;
}
</script>CountdownInput 倒计时输入框
基本用法
<template>
<CountdownInput
v-model:value="phone"
:send-code-api="sendCodeApi"
placeholder="请输入手机号"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { CountdownInput } from '@/components/CountDown';
const phone = ref('');
async function sendCodeApi() {
// 发送验证码
console.log('发送验证码');
return true;
}
</script>Props 属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| value | string | - | 输入框值(支持 v-model) |
| size | 'default' | 'large' | 'small' | 'default' | 组件尺寸 |
| count | number | 60 | 倒计时秒数 |
| sendCodeApi | () => Promise<boolean> | - | 发送验证码的 API 函数 |
继承 Ant Design Vue 的 Input 组件,支持所有 Input 属性(如 placeholder、disabled 等)。
不同尺寸
<template>
<a-space direction="vertical">
<CountdownInput
v-model:value="phone1"
:send-code-api="sendCodeApi"
size="small"
placeholder="小尺寸"
/>
<CountdownInput
v-model:value="phone2"
:send-code-api="sendCodeApi"
size="default"
placeholder="默认尺寸"
/>
<CountdownInput
v-model:value="phone3"
:send-code-api="sendCodeApi"
size="large"
placeholder="大尺寸"
/>
</a-space>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { CountdownInput } from '@/components/CountDown';
const phone1 = ref('');
const phone2 = ref('');
const phone3 = ref('');
async function sendCodeApi() {
return true;
}
</script>useCountdown Hook
如果需要更灵活的倒计时控制,可以使用 useCountdown Hook:
import { useCountdown } from '@/components/CountDown';
const {
start, // 开始倒计时
reset, // 重置倒计时
restart, // 重新开始倒计时
clear, // 清除定时器
stop, // 停止倒计时
currentCount, // 当前倒计时数值 Ref<number>
isStart, // 是否已开始倒计时 Ref<boolean>
} = useCountdown(60); // 参数为倒计时秒数Hook 示例
<template>
<div>
<p>剩余时间:{{ currentCount }} 秒</p>
<a-button @click="start" :disabled="isStart">开始</a-button>
<a-button @click="stop">停止</a-button>
<a-button @click="reset">重置</a-button>
<a-button @click="restart">重新开始</a-button>
</div>
</template>
<script setup lang="ts">
import { useCountdown } from '@/components/CountDown';
const { currentCount, isStart, start, stop, reset, restart } = useCountdown(60);
</script>在 BasicForm 中使用
<template>
<BasicForm @register="registerForm" />
</template>
<script setup lang="ts">
import { h } from 'vue';
import { useForm } from '@/components/Form';
import { CountdownInput } from '@/components/CountDown';
async function sendCodeApi() {
console.log('发送验证码');
return true;
}
const [registerForm] = useForm({
schemas: [
{
field: 'phone',
label: '手机号',
component: 'Input',
rules: [{ required: true, message: '请输入手机号' }],
},
{
field: 'code',
label: '验证码',
component: 'Input',
render: ({ model, field }) => {
return h(CountdownInput, {
value: model[field],
'onUpdate:value': (val) => (model[field] = val),
sendCodeApi: sendCodeApi,
});
},
rules: [{ required: true, message: '请输入验证码' }],
},
],
labelWidth: 100,
});
</script>完整示例:登录表单
<template>
<BasicForm @register="registerForm" />
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { useForm } from '@/components/Form';
import { CountButton } from '@/components/CountDown';
import { message } from 'ant-design-vue';
import { h } from 'vue';
const codeResetKey = ref(0);
const currentPhone = ref('');
async function sendCode() {
// 从表单获取手机号
if (!currentPhone.value) {
message.error('请先输入手机号');
return false;
}
if (!/^1\d{10}$/.test(currentPhone.value)) {
message.error('手机号格式不正确');
return false;
}
try {
// 调用发送验证码接口
// await sendSmsCode(currentPhone.value);
message.success('验证码已发送');
return true;
} catch (error) {
message.error('发送失败');
return false;
}
}
const [registerForm, { getFieldsValue, validate }] = useForm({
schemas: [
{
field: 'phone',
label: '手机号',
component: 'Input',
componentProps: {
placeholder: '请输入手机号',
onChange: (e) => {
currentPhone.value = e.target.value;
},
},
rules: [
{ required: true, message: '请输入手机号' },
{ pattern: /^1\d{10}$/, message: '手机号格式不正确' },
],
},
{
field: 'code',
label: '验证码',
render: () => {
return h('div', { style: { display: 'flex', gap: '8px' } }, [
h(CountButton, {
value: codeResetKey.value,
beforeStartFunc: sendCode,
count: 60,
}),
]);
},
rules: [{ required: true, message: '请输入验证码' }],
},
],
labelWidth: 80,
showActionButtonGroup: true,
submitButtonOptions: {
text: '登录',
},
});
async function handleSubmit() {
try {
const values = await validate();
console.log('登录:', values);
} catch (error) {
console.error('验证失败:', error);
}
}
</script>相关文件
- CountButton:
@/components/CountDown/src/CountButton.vue - CountdownInput:
@/components/CountDown/src/CountdownInput.vue - Hook:
@/components/CountDown/src/useCountdown.ts
相关组件
- [[09. BasicForm 表单]] - 表单容器组件
- [[06. Button 按钮]] - 按钮组件
页面信息›
已优化是