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