最近研究了一下element-ui,想着手动实现一下里面的form组件,贴个组件里面的代码

<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
<el-form-item label="密码" prop="pass">
<el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="确认密码" prop="checkPass">
<el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="年龄" prop="age">
<el-input v-model.number="ruleForm.age"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
<el-button @click="resetForm('ruleForm')">重置</el-button>
</el-form-item>
</el-form>

该组件可以细分为三个组件form,formItem以及input组件,首先介绍input组件

  1. input组件:任务一是实现一个v-model的数据双绑定,即包含value的动态绑定以及input的实现   

           任务二是值发生改变的时候通知formItem组件 

    

<template>
<div>
<input type="text" :value="value" @input="input">
</div>
</template> <script>
export default {
props: {
value: {
type: String,
default: ""
}
},
methods: {
input(e) {
let inputValue = e.target.value;
this.$emit("input", inputValue);
this.$parent.$emit('validate');
}
}
};
</script> <style lang="scss" scoped>
</style>

    2. FormItem组件: 任务一实现一个插槽

            任务二显示label以及校验信息

            任务三  对数据进行校验

<template>
<div>
<label :prop="prop">{{label}}</label>
<slot></slot>
<p>{{error}}</p>
</div>
</template> <script>
//需要知道何时去校验
import Schema from "async-validator";
export default {
inject: ["form"],
props: {
prop: {
type: String,
default: ""
},
label: {
type: String,
default: ""
}
},
data() {
return {
error: ''
}
},
methods: {
validate() {
const rules = this.form.rules[this.prop]; //数组
const value = this.form.model[this.prop];
const descriptor = { [this.prop]: rules };
const schema = new Schema(descriptor);
schema.validate({ [this.prop]: value }, errors => {
if (errors) {
this.error = errors[0].message;
} else {
this.error = "";
}
});
}
},
mounted() {
this.$on("validate", this.validate);
}
};
</script> <style lang="scss" scoped>
</style>

    3.  Form组件: 任务实现rules和model的传递,预留插槽

<template>
<div>
<form>
<slot></slot>
</form>
</div>
</template> <script>
export default {
provide() {
return {
form: this
};
},
props: {
rules: {
type: Object
},
model: {
type: Object
}
}
};
</script> <style lang="scss" scoped>
</style>

完整代码可以访问https://github.com/LinNigo/FormComponent

最新文章

  1. Python爬虫小白入门(三)BeautifulSoup库
  2. apache+php 安装
  3. VIM的一些操作小技巧
  4. [ORM] Entity Framework(1) CodeFirst快速入门
  5. oracle的minus返回第一个表中有、第二个表中没有的数据
  6. CentOS GO 语言环境的搭建
  7. C++Primer 第二章
  8. iOS多语言备选机制
  9. 著名的安装制作软件InnoSetup的源码及示例源码-The installation of a well-known software s source code and sample InnoSetup source
  10. git相关网页
  11. 多线程基本概论multithread
  12. Casperjs/PhantomJs 中文网站截图乱码
  13. PicklingError: Can&#39;t pickle &lt;type &#39;generator&#39;&gt;: it&#39;s not found as __builtin_
  14. Spring Framework 5.0.0.M3中文文档 翻译记录 Part&#160;I.&#160;Spring框架概览1-2.2
  15. Linux PostgreSQL 基础配置指南
  16. Swift - 使用下划线(_)来分隔数值中的数字
  17. JS 之完美运动框架
  18. Canny边缘检测算法的实现
  19. POST和GET的详细解释以及区别
  20. Java限流策略

热门文章

  1. JQuery学习:事件绑定&amp;入口函数&amp;样式控制
  2. ACdream 1112
  3. zoj2562 反素数
  4. DynamicDataDisplay 双击获取坐标
  5. angular和vue的对比学习之路
  6. jedis与spring整合及简单的使用RedisTemplate操作
  7. eclipse svn提交忽略文件及文件夹,ignore设置无效..
  8. Python科学计算生态圈--Pandas
  9. ADSL pppoe 拔号工具rp-pppoe
  10. 你知道forEach和each的区别吗?