Skip to content

CountDownButton 倒计时按钮

用于验证码获取按钮的倒计时功能。

导入

js
import CountDownButton from '@/components/display/countdown/CountDownButton.vue';

使用方法

基础用法

vue
<template>
  <CountDownButton :time="60" :send="send" />
</template>

<script setup>
import { waitTimeOut } from '@imengyu/imengyu-utils';
import CountDownButton from '@/components/display/countdown/CountDownButton.vue';

async function send() {
  await waitTimeOut(900);
  uni.showToast({
    title: '测试发送成功',
    icon: 'success',
  })
}
</script>

保存状态

可以保存当前倒计时的状态,在重新进入页面后,仍然保持倒计时状态。

vue
<template>
  <CountDownButton :time="120" saveState="TestState" :send="send" />
</template>

<script setup>
import { waitTimeOut } from '@imengyu/imengyu-utils';
import CountDownButton from '@/components/display/countdown/CountDownButton.vue';

async function send() {
  await waitTimeOut(900);
  uni.showToast({
    title: '测试发送成功',
    icon: 'success',
  })
}
</script>

自定义渲染

vue
<template>
  <CountDownButton :time="60" :send="send">
    <template #button="{ loading, count, touchable, onClick }">
      <FlexRow align="center">
        <text>获取验证码: </text>
        <Width :size="20" />
        <Button
          type="primary"
          shape="round"
          :text="count > 0 ? `还有 ${count} 秒就可以再拿一次验证码啦` : '点击我获取验证码'"
          :loading="loading"
          :touchable="touchable"
          @click="onClick"
        />
      </FlexRow>
    </template>
  </CountDownButton>
</template>

<script setup>
import { waitTimeOut } from '@imengyu/imengyu-utils';
import CountDownButton from '@/components/display/countdown/CountDownButton.vue';
import Button from '@/components/basic/Button.vue';
import FlexRow from '@/components/layout/FlexRow.vue';
import Width from '@/components/layout/space/Width.vue';

async function send() {
  await waitTimeOut(900);
  uni.showToast({
    title: '测试发送成功',
    icon: 'success',
  })
}
</script>

API 参考

Props

名称说明类型必填默认值
time倒计时时长,单位秒number60
saveState保存倒计时状态的key,用于在页面刷新后恢复倒计时状态string-
countDownText倒计时时显示的文本string'%d 秒后重新获取'
defaultText在未倒计时时显示的文本string'获取验证码'
send发送验证码函数() => Promise<void>-

Slots

名称说明参数
button自定义按钮渲染{ loading: boolean, text: string, count: number, touchable: boolean, onClick: () => void }

Events

名称说明参数
click按钮点击事件$event

主题变量

该组件未定义特定的主题变量,组件继承于 Button 组件的主题变量。