ant design vue input change_vue 表单

文档:https://cn.vuejs.org/v2/guide/forms.html#ad

基本用法

使用vue脚手架

  • 安装Nodejs
  • 安装yarn:https://yarn.bootcss.com/docs/install/#windows-stable
  • 安装 Vue CLI
npm install -g @vue/cli
# OR
yarn global add @vue/cli
  • 创建项目:vue create .
  • yes 后回车

fd644cc817182429b4c5778609aef9e3.png

c01d07c26dc504c41891fb473ea8c556.png

安装完毕,接下来可以运行 vue代码了,运行 yarn serve 实时预览

input

//尝试着对message进行修改:
<input v-model="message" placeholder="edit me">
<p>Message is: {{ message }}</p>

export default {
  name: 'App',
  data(){
    return {
      message: 'hi'
    }
  }
}

07d51c50500e841fa23ccdecbcec40a6.png

改内存的东西页面会跟着改变,这就是双向绑定

textarea

<textarea v-model="message" placeholder="edit me"></textarea>
<p>Message is: {{ message }}</p>

485bf78d51c26e00806a95b24e0633b3.png

checkbox

// 单个绑定布尔值
<label>
    <input type="checkbox" v-model="x">
    <span>x: {{x}}</span>
</label>

// 多个绑定数组
// 单选框用radio,最好加上name,表示是一组的
<div id="app">
    爱好:{{x}}
  <label>
    <input name="want" type="checkbox" v-model="x" :value="1">
    <span>抽烟</span>
  </label>
   <label>
    <input name="want" type="checkbox" v-model="x" :value="2">
    <span>喝酒</span>
  </label>
   <label>
    <input name="want" type="checkbox" v-model="x" :value="3">
    <span>泡妞</span>
  </label>
</div>

export default {
  name: 'App',
  data(){
    return {
      x: []
    }
  }
}

5d2e28ea8ce029d796319df11ff75520.png

321b44cac993ab1e76f6f514766b4e73.png

select

<div id="app">
    想要:{{x}}
    <hr />
    <select v-model="x">
      <option v-for="item in array" :value="item.value" :key="item.value">{{item.text}}</option>
    </select>
</div>

export default {
  name: 'App',
  data(){
    return {
      array: [
        {text: '抽烟', value: 1},
        {text: '喝酒', value: 2},
        {text: '泡妞', value: 3}
      ],
      x: ""
    }
  }
}
// 多选用 multiple,shift+点击,ctrl+点击

form

<div id="app">
    登录
    <form @submit.prevent="onSubmit" action="">
      <label for="">
        <span>用户名</span>
        <input type="text" v-model="user.username" />
      </label>
       <label for="">
        <span>密码</span>
        <input type="password" v-model="user.password"/>
      </label>
      <button type="submit">登录</button>
  </form>
</div>

export default {
  name: 'App',
  data(){
    return {
      user:{
        username: '',
        password: ''
      },
      x: ""
    }
  },
  methods: {
  	onSubmit(){
      console.log(this.user)
    }
  }
}

90279fceeb9e979ed91caeef2130487d.png

想要回车提交必须要有form和button

修饰符

.lazy

<input type="text" v-model.lazy="user.username" />
// 不那么快变化

默认使用 input 事件,input时间包括键盘、鼠标、任何输入设备的输入

.number

只要数字

<input type="text" v-model.number="user.username" />
// 只要数字的名字,如果数据类型是number,那么最好使用.number修饰符

.trim

去掉多余空格

<input type="text" v-model.trim="user.username" />

c577d52c6f03c7f6b497203eafce10e3.png

v-model

等价写法

文档:https://cn.vuejs.org/v2/guide/components-custom-events.html#%E8%87%AA%E5%AE%9A%E4%B9%89%E7%BB%84%E4%BB%B6%E7%9A%84-v-model

<input type="text" v-model.trim="user.username" />
// 相当于
<input type="text" :value="user.username" @input="user.username = $event.target.value"/>
// 如果是 .sync 的话就直接等于 $event

v-model等价写法就是绑定:value等于一个东西,然后@input让这个东西等于 $event.target.value

自定义input

<template>
  <div class="red wrapper">
    <input :value="value" @input="onInput" />
  </div>
</template>

<script>
export default {
  name: 'MyInput',
  props: {
    value: {
      type: String
    }
  },
  methods: {
    onInput(e){
      const value = e.target.value
      this.$emit('input', value)
    }
  }
}
// 要么使用 :value 与 @input 组合,要么使用 v-model

使用 Ant-Design-Vue

安装与引入使用

npm install ant-design-vue --save
// 或者
yarn add ant-design-vue

// 全局引用
import Antd from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css';
Vue.use(Antd);

// 局部引用
import { Button, message } from 'ant-design-vue';
Vue.use(Button);
Vue.prototype.$message = message;

登录框

<template>
  <a-form
    id="components-form-demo-normal-login"
    :form="form"
    class="login-form"
    @submit="handleSubmit"
  >
    <a-form-item>
      <a-input
        v-decorator="[
          'userName',
          { rules: [{ required: true, message: 'Please input your username!' }] },
        ]"
        placeholder="Username"
      >
        <a-icon slot="prefix" type="user" style="color: rgba(0,0,0,.25)" />
      </a-input>
    </a-form-item>
    <a-form-item>
      <a-input
        v-decorator="[
          'password',
          { rules: [{ required: true, message: 'Please input your Password!' }] },
        ]"
        type="password"
        placeholder="Password"
      >
        <a-icon slot="prefix" type="lock" style="color: rgba(0,0,0,.25)" />
      </a-input>
    </a-form-item>
    <a-form-item>
      <a-checkbox
        v-decorator="[
          'remember',
          {
            valuePropName: 'checked',
            initialValue: true,
          },
        ]"
      >
        Remember me
      </a-checkbox>
      <a class="login-form-forgot" href="">
        Forgot password
      </a>
      <a-button type="primary" html-type="submit" class="login-form-button">
        Log in
      </a-button>
      Or
      <a href="">
        register now!
      </a>
    </a-form-item>
  </a-form>
</template>

<script>
export default {
  beforeCreate() {
    this.form = this.$form.createForm(this, { name: 'normal_login' });
  },
  methods: {
    handleSubmit(e) {
      e.preventDefault();
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values);
        }
      });
    },
  },
};
</script>
<style>
#components-form-demo-normal-login .login-form {
  max-width: 300px;
}
#components-form-demo-normal-login .login-form-forgot {
  float: right;
}
#components-form-demo-normal-login .login-form-button {
  width: 100%;
}
</style>

4897bd18aeb5a5c419b779a98277009f.png

校验规则

78c1cc73c65f6ed1e72685fb3de567a8.png

校验示例:

v-decorator="[
  'password',
  { rules: [{ required: true, message: '你丫没填密码!'},{ min: 8, message: '密码最少8个字符,'}, {pattern: /[a-zA-z]/, message: '必须包含至少一个字母'}] }
]"

具体用法看文档:https://antdv.com/docs/vue/introduce-cn/

ant design vue 的 a-modal 使用表单验证可以使用以下步骤: 1. 在 modal 添加一个 Form 组件,并设置其 ref 属性为一个变量名(例如:formRef)。 2. 在 Form 组件添加需要验证表单项,例如 Input、Select 等组件,并设置其 rules 属性为相应的验证规则。 3. 在 modal 的底部添加确定和取消按钮,并在确定按钮的点击事件使用 formRef.validate() 方法来触发表单验证。 4. 如果表单验证通过,则执行相应的操作;否则提示用户错误信息。 以下是一个示例代码: ``` <template> <a-modal title="表单验证" :visible="visible" @cancel="handleCancel" @ok="handleOk"> <a-form :model="formData" :rules="rules" ref="formRef"> <a-form-item label="姓名" :colon="false" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }"> <a-input v-model="formData.name" /> </a-form-item> <a-form-item label="邮箱" :colon="false" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }"> <a-input v-model="formData.email" /> </a-form-item> </a-form> <template slot="footer"> <a-button @click="handleCancel">取消</a-button> <a-button type="primary" @click="handleSubmit">确定</a-button> </template> </a-modal> </template> <script> export default { data() { return { visible: false, formData: { name: '', email: '' }, rules: { name: [{ required: true, message: '请输入姓名', trigger: 'blur' }], email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }, { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }] } } }, methods: { handleCancel() { this.visible = false }, handleOk() { this.$refs.formRef.validate(valid => { if (valid) { // 表单验证通过,执行相应操作 console.log('表单验证通过') this.visible = false } else { // 表单验证失败,提示用户错误信息 console.log('表单验证失败') } }) }, handleSubmit() { // 点击确定按钮,触发表单验证 this.$refs.formRef.validate() } } } </script> ``` 在上述代码,我们使用了 ant design vue 的 a-modal、a-form 和 a-form-item 组件来创建一个包含表单验证的 modal。其,我们通过 ref 属性将 a-form 组件命名为 formRef,然后在确定按钮的点击事件使用 formRef.validate() 方法来触发表单验证。在表单验证通过时,我们执行相应的操作;否则提示用户错误信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值