Vue 子组件调用父组件方法[含参数版]

  • 子组件

<template>
  <div>
    <button @click="childEvent(_obj)">点击调父组件方法</button>
  </div>
</template>
<script>
  export default {
    methods: {
      //   _obj 为参数
      childEvent(_obj) {
        this.$emit('parent-event', _obj);
      }
    }
  };
</script>

子组件发出
this.$emit('parent-event', _obj);

$emit: 发出
parent-event : 方法
_obj : 参数
  • 父组件

<template>
  <div>
    <child @parent-event="parentEvent"></child>
  </div>
</template>
<script>
  import child from './child';
  export default {
    components: {
      child
    },
    methods: {
      parentEvent(_obj) {
        console.log('我是父类方法' + _obj);
      }
    }
  };
</script>

父组件接收
<child @parent-event="parentEvent"></child>
@parent-event : 接收
posted @ 2022-11-15 09:22  IT_IOS_MAN  阅读(68)  评论(0编辑  收藏  举报