当前位置:   article > 正文

vue2+element使用form-create绑定事件_formcreate submitbtn

formcreate submitbtn

首先安装依赖: 

npm i @form-create/element-ui@指定版本号

main.js中引入 

  1. //main.js
  2. import formCreate from '@form-create/element-ui'
  3. Vue.use(formCreate);

使用组件模式,在要使用到的页面写,绑定的数据推荐放在另外的js文件里面统一管理

  1. <form-create
  2. v-model="userForm.formEle"
  3. :rule="userForm.rule"
  4. :option="userForm.option">
  5. </form-create>
  6. <script>
  7. import { userForm } from "../../components/configure/form/form.js";
  8. export default {
  9. data() {
  10. return {
  11. userForm:userForm,
  12. };
  13. },
  14. }
  15. <script>

这是对应的配置js文件

  1. //form.js
  2. export const userForm = {
  3. //实例对象
  4. formEle: {},
  5. //表单数据
  6. formData: {},
  7. //组件参数配置
  8. option: {
  9. form: {
  10. inline: true, //行内表单模式
  11. labelWidth: "auto",
  12. },
  13. row: {
  14. //栅格间隔
  15. gutter: 0,
  16. //布局模式,可选 flex,现代浏览器下有效
  17. type: "",
  18. //flex 布局下的垂直排列方式 top/middle/bottom
  19. align: "middle",
  20. //flex 布局下的水平排列方式 start/end/center/space-around/space-between
  21. justify: "start",
  22. },
  23. submitBtn: false,
  24. },
  25. //表单生成规则
  26. rule: [
  27. {
  28. type: "input",
  29. title: "商品名称",
  30. field: "good",
  31. value: "1",
  32. col: {
  33. span: 12,
  34. labelWidth: 150,
  35. },
  36. props: {
  37. size: "medium",
  38. maxlength: "",
  39. placeholder: "商品名称",
  40. clearable: true,
  41. },
  42. validate: [
  43. { required: true, message: "请输入商品名称", trigger: "blur" },
  44. ],
  45. // on: {
  46. // "blur": function () {
  47. // console.log(22);
  48. // },
  49. // },
  50. emit:['change'],
  51. emitPrefix:'good'
  52. },
  53. ],
  54. btns: [
  55. {
  56. title: "清除条件",
  57. id: "clear",
  58. type: "text",
  59. show: false,
  60. inputSize: "small",
  61. icon: "el-icon-delete",
  62. },
  63. {
  64. title: "查询",
  65. id: "look",
  66. type: "primary",
  67. show: false,
  68. inputSize: "small",
  69. icon: "el-icon-search",
  70. },
  71. {
  72. title: "导出",
  73. id: "export",
  74. type: "primary",
  75. show: false,
  76. inputSize: "small",
  77. icon: "el-icon-plus",
  78. },
  79. ],
  80. };

绑定事件有好几种:

第一种就是注释了的,在配置文件中直接使用on:{},里面写事件,

第二种就是通过emit触发使用的页面的方法,

  1. emit:['change'],//change事件
  2. emitPrefix:'good'//自定义事件前缀(如果不指定,默认就是field的value)
  3. //然后在页面里面通过自定义事件,固定写法,@自定义前缀-事件名="你自己的方法名"
  4. <form-create @good-change="change"/>
  5. //然后再methed里面定义change方法,就好了

觉得上面这种写在标签里不好看的,还有另外一个写法

  1. //在mounted里面通过on监听实例上的good-change方法,后面就是methed里面你自己的方法
  2. mounted () {
  3. this.userForm.formEle.on('good-change',this.change)
  4. },
  5. methods: {
  6. change(){
  7. alert(1)
  8. },
  9. }

监听组件原生事件

  1. emitPrefix:'good',
  2. nativeEmit:['click'],//emit换成nativeEmit
  3. //在页面里面
  4. mounted () {
  5. this.userForm.formEle.on('native-good-click',this.click)//调用methed里面的click方法
  6. },
  7. methods: {
  8. click(){
  9. alert(1)
  10. },
  11. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/797351
推荐阅读
相关标签
  

闽ICP备14008679号