(五) 父子组件结合$emit $on $refs设置重置按钮

一、$emit
1、this $emit(‘自定义时间名’,要传送的数据);
2、触发当前实例上的时间,要传递的数据会传给监听器;

二、$ on
1、VM. o n ( ′ 事 件 名 ′ , c a l l b a c k ) − − − − − − − − − − − − − − − − − − − − c a l l b a c k 回 调 on('事件名',callback) --------------------callback回调 on(,callback)callbackemit要传送的数据FF1B
2、监听当前实例上自定义时间;

父组件:重置按钮

<template>
<common-form class="search-form" ref="thisSearchForm" :model="models"  :forms='forms'>
        <Button type="default" @click="resetForm" v-show="formBtns.reset" >重置</Button>
    </common-form>
</template>
script>
import format from '@/plugins/format.js'
export default {
	methods: {
		resetForm () {
           this.$refs.thisSearchForm.$emit('resetCommonForm')//获取ref为thisSearchForm的节点,触发此子组件的'resetCommonForm'事件
		}
	}
}
</script>

此common-form子组件:

<template>
    <Form
        ref="myCommonForninline"
        :model="forms"
        :rules="ruleValidate"
        :label-width="model.labelWidth?model.labelWidth:90"
        class="my-common-form">
>
</templaste>
<script>
export default {
  name: 'commonForm',
  props: {
    model: {
      type: Object,
      default () {
        return {
          labelWidth: 90,
          data: []
        }
      }
    },
    forms: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  mounted () {
    this.$on('resetCommonForm', function () {//定义'resetCommonForm'函数,触发function里面的函数(将ref绑定为myCommonForninline的内容重置)
      this.$refs.myCommonForninline.resetFields()
    })
  },
}
</script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值