Vant PasswordInput 密碼輸入框

2022-05-31 11:58 更新

介紹

帶網(wǎng)格的輸入框組件,可以用于輸入支付密碼、短信驗證碼等,通常與數(shù)字鍵盤組件配合使用

引入

import Vue from 'vue';
import { PasswordInput, NumberKeyboard } from 'vant';

Vue.use(PasswordInput);
Vue.use(NumberKeyboard);

代碼演示

基礎用法

<!-- 密碼輸入框 -->
<van-password-input
  :value="value"
  info="密碼為 6 位數(shù)字"
  :focused="showKeyboard"
  @focus="showKeyboard = true"
/>
<!-- 數(shù)字鍵盤 -->
<van-number-keyboard
  :show="showKeyboard"
  @input="onInput"
  @delete="onDelete"
  @blur="showKeyboard = false"
/>
export default {
  data() {
    return {
      value: '123',
      showKeyboard: true
    };
  },
  methods: {
    onInput(key) {
      this.value = (this.value + key).slice(0, 6);
    },
    onDelete() {
      this.value = this.value.slice(0, this.value.length - 1);
    }
  }
}

自定義長度

<van-password-input
  :value="value"
  :length="4"
  :gutter="15"
  :focused="showKeyboard"
  @focus="showKeyboard = true"
/>

明文展示

<van-password-input
  :value="value"
  :mask="false"
  :focused="showKeyboard"
  @focus="showKeyboard = true"
/>

錯誤提示

通過error-info屬性可以設置錯誤提示信息,例如當輸入六位時提示密碼錯誤

<!-- 密碼輸入框 -->
<van-password-input
  :value="value"
  :error-info="errorInfo"
  :focused="showKeyboard"
  @focus="showKeyboard = true"
/>
<!-- 數(shù)字鍵盤 -->
<van-number-keyboard
  :show="showKeyboard"
  @input="onInput"
  @delete="onDelete"
  @blur="showKeyboard = false"
/>
export default {
  data() {
    return {
      value: '123',
      showKeyboard: true,
      errorInfo: ''
    };
  },
  methods: {
    onInput(key) {
      this.value = (this.value + key).slice(0, 6);
      if (this.value.length === 6) {
        this.errorInfo = '密碼錯誤';
      } else {
        this.errorInfo = '';
      }
    },
    onDelete() {
      this.value = this.value.slice(0, this.value.length - 1);
    }
  }
}

API

Props

參數(shù)說明類型默認值
value密碼值string''
info輸入框下方文字提示string-
error-info輸入框下方錯誤提示string-
length密碼最大長度number | string6
gutter輸入框格子之間的間距,如 20px 2em,默認單位為pxnumber | string0
mask是否隱藏密碼內(nèi)容booleantrue
focused v2.1.8是否已聚焦,聚焦時會顯示光標booleanfalse

Events

事件名說明回調(diào)參數(shù)
focus輸入框聚焦時觸發(fā)-


實例演示

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號