首页 > 其他分享 >使用form-create监听组件的事件

使用form-create监听组件的事件

时间:2024-06-03 11:23:26浏览次数:10  
标签:form create alert 事件 fApi blur input 监听 change

FormCreate 是一个可以通过 JSON 生成具有动态渲染、数据收集、验证和提交功能的表单生成组件。支持5个UI框架,并且支持生成任何 Vue 组件。内置20种常用表单组件和自定义组件,再复杂的表单都可以轻松搞定

FormCreate官网:https://www.form-create.com

帮助文档:https://form-create.com/v3/


通过配置项`on`,`emit`可以监听组件内抛出的事件

1. 通过配置项`on`监听事件

<template>
  <div>
    {{ text }}
    <form-create :rule="rule" v-model:api="fApi" :option="options"/>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fApi: {},
      text: '没有变化',
      options: {
        onSubmit: (formData) => {
          alert(JSON.stringify(formData))
        }
      },
      rule: [
        {
          type: 'input',
          field: 'event',
          title: 'change 事件',
          on: {
            change: (val) => {
              this.text = '变化了:"' + val.target.value + '"';
            }
          }
        }
      ]
    }

  }
}
</script>
2. 通过配置项`emit`监听事件
通过`emit`方式监听事件时,事件名称中的大写会自动转换成`-`+小写
例如`inputField`组件的 `change`事件,事件名称为`input-field-change`
<template>
  <form-create v-model:api="fApi" :rule="rule" :option="options" @input-field-change="change"
               @input-field2-blur="blur"/>
</template>

<script>
export default {
  data() {
    return {
      fApi: {},
      options: {
        onSubmit: (formData) => {
          alert(JSON.stringify(formData))
        }
      },
      rule: [
        {
          type: 'input',
          field: 'inputField',
          title: 'change 事件',
          emit: ['change']
        },
        {
          type: 'input',
          field: 'inputField2',
          title: 'blur 事件',
          emit: ['blur']
        }
      ]
    }

  },
  methods: {
    change() {
      alert(`change: "${this.fApi.getValue('inputField')}"`)
    },
    blur() {
      alert('blur!')
    }
  }
}
</script>


3. 通过配置项`emitPrefix`自定义事件前缀

事件名称为`${emitPrefix}-${eventName}`
<template>
  <form-create :rule="rule" v-model:api="fApi" :option="options" @prefix1-change="change" @prefix2-blur="blur"/>
</template>

<script>
export default {
  data() {
    return {
      fApi: {},
      options: {
        onSubmit: (formData) => {
          alert(JSON.stringify(formData))
        }
      },
      rule: [
        {
          type: 'input',
          field: 'inputField',
          title: 'change 事件',
          emit: ['change', 'blur'],
          emitPrefix: 'prefix1'
        },
        {
          type: 'input',
          field: 'inputField2',
          title: 'blur 事件',
          emit: ['blur'],
          emitPrefix: 'prefix2'
        }
      ]
    }

  },
  methods: {
    change() {
      alert(`change: "${this.fApi.getValue('inputField')}"`)
    },
    blur() {
      alert('blur!')
    }
  }
}
</script>

 

4. 通过`Api.on`方法监听事件

<template>
  <form-create :rule="rule" v-model:api="fApi" :option="options"/>
</template>

<script>
export default {
  data() {
    return {
      fApi: {},
      options: {
        onSubmit: (formData) => {
          alert(JSON.stringify(formData))
        }
      },
      rule: [
        {
          type: 'input',
          field: 'inputField',
          title: 'change 事件',
          emit: ['change', 'blur'],
          emitPrefix: 'prefix1'
        },
        {
          type: 'input',
          field: 'inputField2',
          title: 'blur 事件',
          emit: ['blur'],
        }
      ]
    }

  },
  methods: {
    change() {
      alert(`change: "${this.fApi.getValue('inputField')}"`)
    },
    blur() {
      alert('blur!')
    }
  },
  mounted() {
    this.fApi.on('prefix1-change', this.change)
    this.fApi.on('input-field2-blur', this.blur)
  }
}
</script>

 

标签:form,create,alert,事件,fApi,blur,input,监听,change
From: https://www.cnblogs.com/xaboy/p/18228411

相关文章

  • 使用form-create时修改表单数据
    FormCreate是一个可以通过JSON生成具有动态渲染、数据收集、验证和提交功能的表单生成组件。支持5个UI框架,并且支持生成任何Vue组件。内置20种常用表单组件和自定义组件,再复杂的表单都可以轻松搞定FormCreate官网:https://www.form-create.com帮助文档:https://form-create.c......
  • 使用form-create生成表单组件
    FormCreate是一个可以通过JSON生成具有动态渲染、数据收集、验证和提交功能的表单生成组件。支持5个UI框架,并且支持生成任何Vue组件。内置20种常用表单组件和自定义组件,再复杂的表单都可以轻松搞定FormCreate官网:https://www.form-create.com帮助文档:https://form-create.c......
  • java中SimpleDateFormat解析日期格式的问题
    在日常写代码的过程中,我们经常要处理各种格式的日期,常见的日期格式有:“20240601”,“2024-06-01”,“2024-6-1”。如何正确地处理日期格式,尤其是对外接口中参数的日期格式,就很重要了,一个不小心就可能出现意想不到的问题。举一个我遇到的真实例子:我们提供的对外接口中有一个参数是......
  • Data日期和DateFormat类
    packageJavaapi.data;/***@authorxiaowang*@creat2024/6/217:25*@DescriptionJavaLotus*/publicclassEmp{privateStringname;privateintage;privateDatabirthday;}Data类:归属包java.util作用:表示日期时间(精确到ms)使用: ......
  • WPF RenderTransform TransformGroup ScaleTransform TranslateTransform
    <Windowx:Class="WpfApp132.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • 翻译《The Old New Thing》- What a drag: Dragging a Uniform Resource Locator (URL
    Whatadrag:DraggingaUniformResourceLocator(URL)andtext-TheOldNewThinghttps://devblogs.microsoft.com/oldnewthing/20080313-00/?p=23123RaymondChen 2008年03月13日 麻烦的拖拽:拖拽统一资源定位符(URL)和文本简要        这篇文章主要讲......
  • 翻译《The Old New Thing》- What a drag: Dragging a Uniform Resource Locator (URL
    Whatadrag:DraggingaUniformResourceLocator(URL)-TheOldNewThing(microsoft.com)https://devblogs.microsoft.com/oldnewthing/20080312-00/?p=23133RaymondChen 2008年03月12日麻烦的拖拽:拖拽统一资源定位符(URL)简要本文介绍了如何在Windows程序中实......
  • 深度学习论文翻译解析(二十二):Uniformed Students Student-Teacher Anomaly Detection W
    论文标题:UniformedStudentsStudent-TeacherAnomalyDetectionWithDiscriminativeLatentEmbbeddings论文作者: PaulBergmann MichaelFauser DavidSattlegger CarstenSteger论文地址:https://openaccess.thecvf.com/content_CVPR_2020/papers/Bergmann_Uninformed......
  • Transformer 模型完全解读:代码+注释+讲解
    节前,我们组织了一场算法岗技术&面试讨论会,邀请了一些互联网大厂朋友、今年参加社招和校招面试的同学。针对大模型技术趋势、大模型落地项目经验分享、新手如何入门算法岗、该如何准备面试攻略、面试常考点等热门话题进行了深入的讨论。总结链接如下:重磅消息!《大模型面试......
  • Terraform管理OpenStack
      官方安装指南https://developer.hashicorp.com/terraform/installhttps://developer.hashicorp.com/terraform/intro/getting-started/install.html安装sudoyuminstall-yyum-utilssudoyum-config-manager--add-repohttps://rpm.releases.hashicorp.com/RHEL/h......