组合式函数
组合式函数是 Vue 3 中引入的一个新功能,它允许我们在组件中使用函数的方式来组织代码。
函数列表
函数名 | 说明 |
---|---|
usePureCodeCountdown() | [详情] 验证码倒计时 |
usePureCodeCountdown()
验证码倒计时。
参数
参数是一个对象,对象属性如下:
属性名 | 类型 | 默认值 | 说明 |
---|---|---|---|
key | String | "default" | 验证码的 key ,用于区分不同的验证码 |
seconds | Number | 60 | 倒计时的秒数 |
end | Function | null | 倒计时结束后的回调函数 |
change | Function | null | 倒计时描述变化时的回调函数,参数为剩余秒数 |
返回值
返回一个对象,对象属性如下:
类型 | 说明 |
---|---|
seconds | 倒计时的剩余秒数 |
isCountdown | 是否正在倒计时 |
start | 开始函数 |
clear | 清除函数 |
示例
js
import { ref } from "vue";
import { usePureCodeCountdown } from "@/uni_modules/pure-utils";
const tips = ref("获取验证码");
// seconds: 剩余秒数
// isCountdown: 是否正在倒计时
// start: 开始函数
// clear: 清除函数
const { seconds, isCountdown, start, clear } = usePureCodeCountdown({
key: "page-login",
seconds: 30,
end: onEnd,
change: onChange,
});
// 开始倒计时
// 实际使用时通过按钮事件触发调用
start();
// 倒计时结束后的回调
function onEnd() {
console.log("倒计时结束了");
tips.value = "重新发送验证码";
}
// 倒计时更新时的回调函数
function onChange(seconds) {
tips.value = `${seconds}s后重新获取`;
}