Input 输入框组件

介绍

单行输入框

安装

  1. import { createApp } from 'vue';
  2. import { Input } from '@nutui/nutui';
  3. const app = createApp();
  4. app.use(Input);

代码演示

基础用法

双向绑定

  1. <nut-input
  2. v-model="state.val1"
  3. @change="change"
  4. @focus="focus"
  5. @blur="blur"
  6. label="文本"
  7. />
  8. <nut-input placeholder="请输入文本"
  9. @change="change"
  10. v-model="state.val0"
  11. :require-show="true"
  12. label="文本"
  13. @clear="clear"
  14. />

禁用和只读

  1. <nut-input v-model="state.val2" @change="change" disabled="true" label="标题:"/>
  2. <nut-input v-model="state.val3" @change="change" readonly="true" label="标题:"/>

限制输入长度

  1. <nut-input v-model="state.val4" @change="change" max-length="7" label="限制7" />

其他类型

  1. <nut-input v-model="state.val0" @change="change" type="password" label="密码"/>
  2. <nut-input v-model="state.val5" @change="change" type="number" label="整数" />
  3. <nut-input v-model="state.val6" @change="change" type="digit" placeholder="支持小数点的输入" label="数字"/>

代码

  1. import { reactive } from 'vue';
  2. import { createComponent } from '@/packages/utils/create';
  3. const { createDemo } = createComponent('input');
  4. export default createDemo({
  5. setup() {
  6. const state = reactive({
  7. val0: '',
  8. val1: '初始数据',
  9. val2: '禁止修改',
  10. val3: 'readonly 只读',
  11. val4: '',
  12. val5: '',
  13. val6: '',
  14. val7: '',
  15. val8: '文案'
  16. });
  17. setTimeout(function() {
  18. state.val1 = '异步数据';
  19. }, 2000);
  20. const change = (value: string | number,event:Event) => {
  21. console.log('change: ', value,event);
  22. };
  23. const focus = (value: string | number,event:Event) => {
  24. console.log('focus:', value,event);
  25. };
  26. const blur = (value: string | number,event:Event) => {
  27. console.log('blur:', value,event);
  28. };
  29. const clear = (value: string | number) => {
  30. console.log('clear:', value);
  31. };
  32. return {
  33. state,
  34. change,
  35. blur,
  36. clear,
  37. focus
  38. };
  39. }
  40. });

Prop

参数说明类型默认值
v-model输入值,双向绑定String-
type类型,可选值为 text numberStringtext
placeholder为空时占位符String-
label左侧文案String-
require-show左侧*号是否展示Booleanfalse
disabled是否禁用Booleanfalse
readonly是否只读Booleanfalse
max-length限制最长输入字符String、Number-
clearable展示清除iconBooleantrue
text-align文本位置,可选值left,center,rightStringleft

Event

名称说明回调参数
change输入内容时触发val ,event
focus聚焦时触发val ,event
blur失焦时触发val ,event
clear点击清空时触发val

Input  输入框 - 图1