欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一文詳細(xì)分析Vue3中的emit用法(子傳父)

 更新時間:2024年05月28日 11:03:17   作者:碼農(nóng)研究僧  
Emit是Vue3中另一種常見的組件間傳值方式,它通過在子組件中觸發(fā)事件并將數(shù)據(jù)通過事件參數(shù)傳遞給父組件來實現(xiàn)數(shù)據(jù)傳遞,這篇文章主要給大家介紹了關(guān)于詳細(xì)分析Vue3中emit用法(子傳父)的相關(guān)資料,需要的朋友可以參考下

1. 基本知識

在 Vue 3 中,emit 是一種機(jī)制,用于在子組件中觸發(fā)事件,并在父組件中監(jiān)聽這些事件

提供一種組件間通信的方式,尤其是在處理父子組件數(shù)據(jù)傳遞和交互時非常有用

一共有兩種方式

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>

父組件監(jiān)聽子組件:

<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>

父組件監(jiān)聽子組件:

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

  • 創(chuàng)建子組件:
<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>
  • 創(chuàng)建父組件:
<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>
  • 應(yīng)用組件:
<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');

總結(jié) 

到此這篇關(guān)于Vue3中的emit用法(子傳父)的文章就介紹到這了,更多相關(guān)Vue3中emit用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論