当前位置: 代码网 > it编程>编程语言>Javascript > 一文详细分析Vue3中的emit用法(子传父)

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

2024年06月11日 Javascript 我要评论
1. 基本知识在 vue 3 中,emit 是一种机制,用于在子组件中触发事件,并在父组件中监听这些事件提供一种组件间通信的方式,尤其是在处理父子组件数据传递和交互时非常有用一共有两种方式1.1 em

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如下:

  • 创建子组件:
<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" />
    <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>
  • 应用组件:
<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');

总结 

到此这篇关于vue3中的emit用法(子传父)的文章就介绍到这了,更多相关vue3中emit用法内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com