Skip to content

Props 和事件

<OtpInput> 组件的完整 API 参考。

Props

核心

Prop类型默认值描述
modelValuestring''OTP 值的 v-model 绑定
numInputsnumber6输入框数量
inputType'number' | 'tel' | 'letter-numeric' | 'password''tel'输入验证类型
inputmodestring'numeric'虚拟键盘的 HTML inputmode

外观

Prop类型默认值描述
themeOtpTheme'box'视觉主题(见 主题
variant'default' | 'primary' | 'success' | 'danger' | 'warning''default'颜色变体
size'sm' | 'md' | 'lg' | 'xl''md'输入框大小
placeholderstring | string[]''占位符(字符串或每个输入框的数组)
separatorstring''输入框之间渲染的 HTML

状态

Prop类型默认值描述
shouldAutoFocusbooleanfalse挂载时自动聚焦第一个输入框
shouldFocusOrderbooleanfalse强制顺序输入
disabledbooleanfalse禁用所有输入框
readonlybooleanfalse只读模式
errorbooleanfalse错误状态(触发抖动动画)
successbooleanfalse成功状态(触发脉冲动画)

样式

Prop类型默认值描述
inputClassesstring | string[]''每个输入框的 CSS 类
conditionalClassstring[] | (index, value) => string每个输入框的类(数组或函数)
containerClassstring''容器的 CSS 类
ariaLabelstring'OTP Input'无障碍标签

事件

事件载荷描述
update:modelValuestring每次变化(v-model 支持)
changestring每次字符变化
completestring所有输入框填满

暴露的方法

通过模板 ref 访问:

vue
<script setup>
const otpRef = ref(null)

otpRef.value?.clear()        // 清空所有输入框,聚焦第一个
otpRef.value?.fill('123456') // 填充所有输入框
otpRef.value?.focus()        // 聚焦第一个输入框
otpRef.value?.focusInput(3)  // 按索引聚焦特定输入框
</script>

<template>
  <OtpInput ref="otpRef" />
</template>
方法参数描述
clear()清空所有输入框并聚焦第一个
fill(value)string用给定值填充输入框
focus()聚焦第一个输入框
focusInput(index)number按索引聚焦特定输入框

TypeScript

所有类型均已导出:

ts
import type {
  OtpInputProps, OtpInputExpose, OtpInputType,
  OtpTheme, OtpVariant, OtpSize,
} from 'vue-otp-pro'