详细分析Vue3中的emit用法(子传父)

1. 基本知识

在 Vue 3 中,emit 是一种机制,用于在子组件中触发事件,并在父组件中监听这些事件

提供一种组件间通信的方式,尤其是在处理父子组件数据传递和交互时非常有用

一共有两种方式

1.1 emit

子组件中使用emit

<template>
  <button @click="handleClick">Click me</button>
</template>

<script>
export default {
  name: 'ChildComponent',
  methods: {
    handleClick() {
      this.$emit('custom-event', 'Hello from child');
    }
  }
}
</script>

父组件监听子组件:

<template>
  <div>
    <ChildComponent @custom-event="handleCustomEvent" />
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  name: 'ParentComponent',
  components: {
    ChildComponent
  },
  methods: {
    handleCustomEvent(payload) {
      console.log(payload); // 输出 'Hello from child'
    }
  }
}
</script>

1.2 defineEmits

在 Vue 3 中,还可以使用 Composition API 的 defineEmits 方法来定义和使用 emit

子组件中定义和使用emit:

<template>
  <button @click="emitEvent">Click me</button>
</template>

<script setup>
import { defineEmits } from 'vue';

const emit = defineEmits(['custom-event']);

function emitEvent() {
  emit('custom-event', 'Hello from child with Composition API');
}
</script>

父组件监听子组件:

<template>
  <div>
    <ChildComponent @custom-event="handleCustomEvent" />
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  name: 'ParentComponent',
  components: {
    ChildComponent
  },
  methods: {
    handleCustomEvent(payload) {
      console.log(payload); // 输出 'Hello from child with Composition API'
    }
  }
}
</script>

2. Demo

完整Demo如下:

  1. 创建子组件:
<template>
  <button @click="emitEvent">Click me</button>
</template>

<script setup>
import { defineEmits } from 'vue';

const emit = defineEmits(['custom-event']);

function emitEvent() {
  emit('custom-event', 'Hello from child with Composition API');
}
</script>
  1. 创建父组件:
<template>
  <div>
    <ChildComponent @custom-event="handleCustomEvent" />
    <p>{{ message }}</p>
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';
import { ref } from 'vue';

export default {
  name: 'ParentComponent',
  components: {
    ChildComponent
  },
  setup() {
    const message = ref('');

    function handleCustomEvent(payload) {
      message.value = payload;
    }

    return {
      message,
      handleCustomEvent
    };
  }
}
</script>
  1. 应用组件:
<template>
  <ParentComponent />
</template>

<script>
import ParentComponent from './components/ParentComponent.vue';

export default {
  name: 'App',
  components: {
    ParentComponent
  }
}
</script>

主入口文件:

import { createApp } from 'vue';
import App from './App.vue';

createApp(App).mount('#app');
  • 13
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农研究僧

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值