Skip to content

SendCode 发送验证码

代码演示

基础用法

vue
<template>
    <div>
        <x-send-code ref="sendCodeRef">
            <template #default="{ running, seconds, disabled }">
                <a-button
                    :disabled="disabled"
                    @click="handleSend">
                    <template v-if="running">{{ seconds }}s 后重新获取</template>
                    <template v-else>发送验证码</template>
                </a-button>
            </template>
        </x-send-code>
    </div>
</template>

<script setup>
import { ref } from 'vue'

const sendCodeRef = ref()

function handleSend() {
    sendCodeRef.value.start()
}
</script>

<style lang="less" scoped></style>

重置

vue
<template>
    <div>
        <a-space>
            <x-send-code ref="sendCodeRef">
                <template #default="{ running, seconds, disabled }">
                    <a-button
                        :disabled="disabled"
                        @click="handleSend">
                        <template v-if="running">{{ seconds }}s 后重新获取</template>
                        <template v-else>发送验证码</template>
                    </a-button>
                </template>
            </x-send-code>
            <template v-if="allowReset">
                <a-button @click="handleReset">重置</a-button>
            </template>
        </a-space>
    </div>
</template>

<script setup>
import { ref } from 'vue'

const sendCodeRef = ref()
const allowReset = ref(false)

function handleSend() {
    sendCodeRef.value.start()
    allowReset.value = true
}

function handleReset() {
    sendCodeRef.value.reset()
    allowReset.value = false
}
</script>

<style lang="less" scoped></style>

自定义

vue
<template>
    <div>
        <a-input :style="{ width: '260px' }">
            <template #suffix>
                <x-send-code
                    ref="sendCodeRef"
                    :seconds="10">
                    <template #default="{ running, seconds }">
                        <template v-if="running">{{ seconds }}s 后重新获取</template>
                        <template v-else>
                            <a @click="handleSend"> 发送验证码 </a>
                        </template>
                    </template>
                </x-send-code>
            </template>
        </a-input>
    </div>
</template>

<script setup>
import { ref } from 'vue'

const sendCodeRef = ref()

function handleSend() {
    sendCodeRef.value.start()
}
</script>

<style lang="less" scoped></style>

API

Props

参数说明类型默认值
seconds(v-model)计时时长number60

Events

事件名说明回调参数
change计时发生改变seconds: number
start计时开始-
end计时结束-
reset重置计时-

Methods

通过 ref 可以获取到 SendCode 实例并调用实例方法。

方法名说明参数返回值
start开始计时--
reset重置--

Slots

名称说明参数
default默认插槽{seconds: number, running: boolean, disabled: boolean}

本文档内容版权为 XYAdmin 作者所有,保留所有权利。