ant design vue 之 v-decorator

1.在 template 中直接使用

required: true 设置必填, trigger: 'blur' 设置失去焦点触发校验
<a-input
  v-decorator="['name', { rules: [{ required: true, message: '请输入账号!'}],trigger: 'blur' }]" 
 />

2.在 script 中设置校验规则

template 部分
 <a-input
   v-decorator="['password', validatorRules.password]"
 />
script 部分
   data(){
    return {
       form: this.$form.createForm(this),
       validatorRules: {
          password : {//name与v-decorator中属性对应
            initialValue: 123456,//初始值
            rules: [
              { required: true, message: '请输入密码!'},//此处开启校验必填
              { min: 5, message: '长度不少于 5 个字符'}, // 长度校验
              { validator: this.validatePassword},//校验规则函数,
            ],
            trigger: 'blur' // 触发方式
          },
        },
    }
  },
   methods: {
      validatePassword(rule, value, callback) {
      
      }
  }

3. 完整代码

<template>
  <div class="login">
    <a-form :form="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }" @submit="handleSubmit">
    <a-form-item label="name">
      <!-- 直接写检验规则 -->
      <a-input
         v-decorator="['name', { rules: [{ required: true, message: '请输入账号!'}],trigger: 'blur' }]" 
      />
    </a-form-item>
    <a-form-item label="password">
      <!-- 在script 里面定义更复杂的检验规则,  -->
      <a-input
        v-decorator="['password', validatorRules.password]"
      />
    </a-form-item>
    <a-form-item :wrapper-col="{ span: 12, offset: 5 }">
      <a-button type="primary" html-type="submit">
        Login
      </a-button>
    </a-form-item>
  </a-form>
  </div>
</template>

<script>
export default {
  name: 'login',
  data(){
    return {
       form: this.$form.createForm(this),
       validatorRules: {
          password : {//name与v-decorator中属性对应
            initialValue: 123456,//初始值
            rules: [
              { required: true, message: '请输入密码!'},//此处开启校验必填
              { min: 5, message: '长度不少于 5 个字符'}, // 长度校验
              { validator: this.validatePassword},//校验规则函数,
            ],
            trigger: 'blur' // 触发方式
          },
        },
    }
  },
  created(){
    // this.$nextTick(() => {
    //     this.form.setFieldsValue({name:'yyy',password: 123});
    //   })
  },
  mounted(){
    this.form.setFieldsValue({name:'yyy',password: 123});
  },
  methods: {
     handleSubmit(e) {
      e.preventDefault();
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values);
        }
      });
    },
    validatePassword(rule, value, callback) {
      
    }
  }
}
</script>

注意:

确保使用 this.form.setFieldsValue({name:'yyy',password: 123}) 设置值
或者 this.form.validateFields((err, values) => { }); 获取值
之前
已经 this.$form.createForm(this)

如果在钩子函数里面使用 setFieldsValue 报类似错误

warning.js?d96e:34 Warning: You cannot set a form field before rendering a field associated with the value. You can use `getFieldDecorator(id, options)` instead `v-decorator="[id, options]"` to register it before render.

可以这样试下

this.$nextTick(() => {
  this.form.setFieldsValue({name:'yyy',password: 123});
})
  • 8
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,我会依次回答你的问题。 ① 首先,你需要安装Apache服务器,可以使用以下命令: ``` sudo apt-get update sudo apt-get install apache2 ``` 安装完成后,使用以下命令启动Apache服务器: ``` sudo systemctl start apache2 ``` 然后,使用127.0.0.1测试服务器是否能够正常运行: 在浏览器中输入127.0.0.1,如果能够正常打开Apache的默认页面,说明服务器已经正常运行。 ② 修改服务器的域名为localhost,并在/var/www/html文件夹内写入一个主页index.html,测试服务器是否能够正常运行,且正常打开index.html。 将Apache的配置文件/etc/apache2/sites-available/000-default.conf中的ServerName改为localhost,然后在/var/www/html文件夹内写入一个主页index.html,内容可以随意编写。 使用以下命令重启Apache服务器: ``` sudo systemctl restart apache2 ``` 在浏览器中输入localhost,如果能够正常打开index.html页面,说明服务器已经正常运行。 ③ 创建文件夹/home/www,并且创建首页文件myweb.html。在主配置文件/etc/httpd/conf/httpd.conf中,修改网站数据目录、首页文件名参数,测试打开的网页是否是myweb.html。 首先创建文件夹/home/www,并在其中创建一个名为myweb.html的文件,内容可以随意编写。 然后,打开/etc/apache2/apache2.conf文件,找到以下两行: ``` # Global configuration # ``` 在下面添加以下内容: ``` ServerName localhost DocumentRoot /home/www <Directory /home/www> AllowOverride None Require all granted </Directory> ``` 保存文件并重启Apache服务器: ``` sudo systemctl restart apache2 ``` 在浏览器中输入localhost,如果能够正常打开myweb.html页面,说明服务器已经正常运行。 ④ 结合上题的DNS服务器,设置虚拟主机,创建文件夹,文件夹路径为:/名字拼音的首字母/www,在文件夹内创建首页index.html,修改Apache服务主配置文件。 首先创建文件夹/名字拼音的首字母/www,并在其中创建一个名为index.html的文件,内容可以随意编写。 然后,在/etc/apache2/sites-available/目录下创建一个名为名字拼音的首字母.conf的配置文件,内容如下: ``` <VirtualHost *:80> ServerName www.名字拼音的首字母.com ServerAlias 名字拼音的首字母.com DocumentRoot /名字拼音的首字母/www <Directory /名字拼音的首字母/www> AllowOverride None Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> ``` 然后使用以下命令激活该虚拟主机: ``` sudo a2ensite 名字拼音的首字母.conf ``` 最后重启Apache服务器: ``` sudo systemctl restart apache2 ``` ⑤ 在本机或客户端测试使用www. 名字拼音的首字母.com域名是否能打开页面。 在浏览器中输入www.名字拼音的首字母.com,如果能够正常打开index.html页面,说明虚拟主机已经正常配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值