Custom form components

The components that implement v-model can all be called form components

Form components can modify the component state through methods such as $ f.formData, $ f.getValue, $ f.setValue, $ f.disabled, etc.

预定义

Define the following properties and events to achieve the same effect as the built-in components.

props

Receive properties via custom properties inside custom components

  • value Component value
  • disabled Disabled state of the component

E.g:

  1. vm = Vue({
  2. props:{
  3. value:String,
  4. disabled:Boolean
  5. }
  6. })

input event

Update the value inside the component through the input event

When the component value changes, update the value through the input event. For example:

  1. vm.$emit('input',newValue);

Mounting custom components

The custom component to be generated must be mounted globally via the vue.component method, or via theformCreate.component method

E.g:

  1. formCreate.component('TestComponent',component);

生成

The form component must define the field attribute

JSON

  1. {
  2. type:'TestComponent',
  3. value:'test',
  4. field:'testField',
  5. title:'Custom form component'
  6. }

Maker

  1. formCreate.maker.create('TestComponent','testField','Custom form component').value('test')

Now this custom component can be operated like the built-in component.

Example

Customize the counter button component and get the number of button clicks. The function of this component is the same as the built-in component

::: demo

  1. <template>
  2. <form-create :rule="rule" v-model="fApi" :option="options"/>
  3. </template>
  4. <script>
  5. export default {
  6. data(){
  7. return {
  8. fApi:{},
  9. options:{
  10. onSubmit:(formData)=>{
  11. alert(JSON.stringify(formData));
  12. },
  13. resetBtn:true
  14. },
  15. rule:[
  16. this.$formCreate.maker.template('<el-button @click="onClick" long :disabled="disabled">counter-{{num}}</el-button>', function(){
  17. return new Vue({
  18. data: function () {
  19. return {
  20. num: this.value,
  21. }
  22. },
  23. props:{
  24. //Predefined
  25. disabled:Boolean,
  26. value:Number,
  27. },
  28. watch:{
  29. value(n){
  30. //Sync value
  31. this.num = n;
  32. }
  33. },
  34. methods: {
  35. onClick: function () {
  36. this.num++;
  37. //Update the value inside the component
  38. this.$emit('input',this.num);
  39. },
  40. },
  41. });
  42. },'btn','Custom form components').value(10)
  43. ]
  44. }
  45. }
  46. }
  47. </script>