大家工作当中,总会要写各种各样的表单,比如:登录,注册,客户评价,反馈意见等,我也一样,总是重复不断的写,要是有一个固定的模版,每次套入一下就好了,就不用重复不断的做相同的工作了。
主要思路是用到渲染函数(render),个性化配置项目所需的表单元素。
渲染函数中的第一个参数是 createElement,其接受的参数如下:
第一个参数:可以是一个 HTML 标签名、组件选项对象,或者 resolve 了上述任何一种的一个 async 函数。必填项。这里我们挂载的是我们的 Input 组件等
createElement(
// {String | Object | Function}
// 一个 HTML 标签名、组件选项对象,或者
// resolve 了上述任何一种的一个 async 函数。必填项。
'div',
// {Object}
// 一个与模板中 attribute 对应的数据对象。可选。
{
},
// {String | Array}
// 子级虚拟节点 (VNodes),由 `createElement()` 构建而成,
// 也可以使用字符串来生成“文本虚拟节点”。可选。
[
'字符串来生成文本虚拟节点', createElement('h1', '一串字符串'), createElement(MyComponent, {
props: {
someProp: 'foobar'
}
})
]
)
第二个参数 :Object ,我们可以在这个 Object 中指定相关的属性值,比如 class、style、attrs(普通的 HTML attribute)、组件的 props。具体如下:
{
// 与 `v-bind:class` 的 API 相同,
// 接受一个字符串、对象或字符串和对象组成的数组
'class': {
foo: true,
bar: false
},
// 与 `v-bind:style` 的 API 相同,
// 接受一个字符串、对象,或对象组成的数组
style: {
color: 'red',
fontSize: '14px'
},
// 普通的 HTML attribute
attrs: {
id: 'foo'
},
// 组件 prop
props: {
myProp: 'bar'
},
// DOM property
domProps: {
innerHTML: 'baz'
},
// 事件监听器在 `on` 内,
// 但不再支持如 `v-on:keyup.enter` 这样的修饰器。
// 需要在处理函数中手动检查 keyCode。
on: {
click: this.clickHandler
},
// 仅用于组件,用于监听原生事件,而不是组件内部使用
// `vm.$emit` 触发的事件。
nativeOn: {
click: this.nativeClickHandler
},
// 自定义指令。注意,你无法对 `binding` 中的 `oldValue`
// 赋值,因为 Vue 已经自动为你进行了同步。
directives: [
{
name: 'my-custom-directive',
value: '2',
expression: '1 + 1',
arg: 'foo',
modifiers: {
bar: true
}
}
],
// 作用域插槽的格式为
// { name: props => VNode | Array<VNode> }
scopedSlots: {
default: props => createElement('span', props.text)
},
// 如果组件是其它组件的子组件,需为插槽指定名称
slot: 'name-of-slot',
// 其它特殊顶层 property
key: 'myKey',
ref: 'myRef',
// 如果你在渲染函数中给多个元素都应用了相同的 ref 名,
// 那么 `$refs.myRef` 会变成一个数组。
refInFor: true
}
下面继续公共组件FormItem:
<template>
<div class="hello">
<div v-for="(config, index) in configJsonArr" :key="config.type + index">
<form-item :configJson="config"></form-item>
</div>
</div>
</template>
<script>
import Input from './Input'import Select from './Select'
const FormItem = {
components: {
Input, Select
},
name: 'FormItem',
props: {
// 传入配置
configJson: {
required: true
}
},
// h 实际上就是 createElement 参数
render (h) {
// 第一个参数就是配置中的 type,也就是我们的组件名称
return h(`${this.configJson.type}`, {
props: {
// 针对 props 进行解构
...this.configJson.props || {}
},
attrs: {
// 针对 attrs 进行解构
...this.configJson.props || {}
}
})
}
}
export default { name: 'Config', components: { FormItem }, props: { configJsonArr: { type: Array, required: true, default: () => [] } }}</script>
FormItem组件里面有2个组件:Input, Select,若是表单里面还需要别的元素,可以继续添加,比如Radio,CheckBox等,这里暂时只添加这两个做事例。下面是Input, Select具体代码:
//Input组件
<template>
<div class="hello input-con">
<label>输入框:</label>
<el-input v-bind="$attrs"></el-input>
</div>
</template>
<script>
export default {
name: 'Input'
}
</script>
//Select组件
<template>
<div class="hello select-con">
<label>选择框:</label>
<el-select v-bind="configJson.props" v-model="value"></el-select>
</div>
</template>
<script>
export default {
name: 'Select',
data () {
return { value: '' }
}
}
</script>
el-input 上的v-bind="$attrs",可以使父组件的参数传递到el-input组件上,也可以换一种写法:v-bind=“configJson.props”.
代码完整版见:gitee.com/yp138163962…