最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue3中emit傳值的具體使用

 更新時間:2023年12月21日 10:27:03   作者:Python私教  
Emit是Vue3中另一種常見的組件間傳值方式,它通過在子組件中觸發(fā)事件并將數(shù)據(jù)通過事件參數(shù)傳遞給父組件來實現(xiàn)數(shù)據(jù)傳遞,本文就來介紹一下Vue3 emit傳值,感興趣的可以了解一下

概述

We have already seen that props are used to pass data from a parent component to a child component. To pass data from a child component back to a parent component, Vue offers custom events.

我們已經(jīng)看到,道具用于將數(shù)據(jù)從父組件傳遞到子組件。為了將數(shù)據(jù)從子組件傳遞回父組件,Vue 提供了自定義事件。

In a component, we can emit an event using the e m i t m e t h o d ; w i t h t h i s . emit method; with this. emitmethod;withthis.emit(‘eventName’, payload) within <script>; or just with $emit within the template section.

在組件中,我們可以使用 e m i t 方法、在 ‘ < s c r i p t > ‘ 中使用 t h i s . emit 方法、在 `<script>` 中使用 this. emit方法、在‘<script>‘中使用this.emit(‘eventName’, payload) 或在模板部分使用 $emit 來發(fā)射事件。

Assuming we have got a reactive instance property, this.message, we could emit a send event with the message value in the script section using this.$emit. This could be the basis for a MessageEditor component:

假設我們已經(jīng)有了一個反應式實例屬性 this.message,我們就可以在腳本部分使用 this.$emit 發(fā)送一個帶有消息值的發(fā)送事件。這可以作為 MessageEditor 組件的基礎:

<script>
export default {
  data () {
    return {
      message: null
    }
  },
  methods: {
    send() {
      this.$emit('send', this.message);
    }
  }
}
</script>

In the same scenario, we could trigger a send event from the template section as follows:

在同樣的情況下,我們可以從模板部分觸發(fā)發(fā)送事件,如下所示:

<template>
  <div>
    <input v-model="message" />
    <button @click="$emit('send', message)">Emit inline</button>
  </div>
</template>

From a parent component, we can use v-on:event-name or the shorthand @event-name. event-name must match the name passed to $emit. Note eventName and event-name are not equivalent.

在父組件中,我們可以使用 v-on:event-name 或快捷方式 @event-name。event-name 必須與傳遞給 $emit 的名稱一致。請注意,eventName 和 event-name 并不等同。

For instance, in the parent component we want to listen to the send event and modify some data accordingly. We bind @send with some event handler logic, which can be a JavaScript expression or a method declared using methods.

例如,在父組件中,我們要監(jiān)聽發(fā)送事件并相應地修改一些數(shù)據(jù)。我們將 @send 與一些事件處理邏輯綁定,這些邏輯可以是 JavaScript 表達式,也可以是使用 methods 聲明的方法。

Vue will trigger this event handler and pass the event’s payload object to it when applicable. You can use $event in the JavaScript expression of the template as the payload, as shown in the following example of the template section in App:

Vue 將觸發(fā)該事件處理程序,并在適用時將事件的有效載荷對象傳遞給它。您可以在模板的 JavaScript 表達式中使用 $event 作為有效載荷,如以下 App.Vue 中模板部分的示例所示:

<template>
  <div id="app">
    <p>Message: {{ parentMessage }}</p>
    <MessageEditor @send="parentMessage = $event" />
    <button @click="parentMessage = null">Reset</button>
  </div>
</template>

We can also extract the JavaScript expression to a component’s updateParentMessage method and bind it as follows:

我們還可以將 JavaScript 表達式提取到組件的 updateParentMessage 方法中,并將其綁定如下:

<template>
  <div id="app">
    <p>Message: {{ parentMessage }}</p>
    <MessageEditor @send="updateParentMessage" />
    <button @click="parentMessage = null">Reset</button>
  </div>
</template>
<script>
import MessageEditor from './components/MessageEditor.vue'
export default {
  components: {
    MessageEditor
  },
  data() {
    return {
      parentMessage: null
    }
  },
  methods: {
    updateParentMessage(newMessage) {
      this.parentMessage = newMessage
    }
  }
}
</script>

Custom events support passing any JavaScript type as the payload. The event name, however, must be a String.

自定義事件支持傳遞任何 JavaScript 類型作為有效載荷。不過,事件名稱必須是字符串。

在setup中使用事件

If you use <script setup>, since there is no component’s options object, we can’t define custom events using the emits field. Instead, we use the defineEmits() function from the vue package and pass all the relevant events’ definitions to it.

如果使用 <script setup>,由于沒有組件的選項對象,我們就無法使用 emits 字段定義自定義事件。相反,我們可以使用 vue 軟件包中的 defineEmits() 函數(shù),并將所有相關事件的定義傳遞給它。

For example, in the MessageEditor component, we can rewrite the event-registering functionality with defineEmits() as follows:

例如,在 MessageEditor 組件中,我們可以使用 defineEmits() 重寫事件注冊功能如下:

<template>
  <div>
    <input v-model="message"/>

    <!--點擊的適合,手動傳遞值-->
    <button @click="$emit('send', message)">Emit inline</button>
  </div>
</template>

<script setup>
import {defineEmits, ref} from 'vue'

const message = ref(null)

// 定義事件
const emits = defineEmits(['send'])

// 通過事件向父組件傳遞值
emits('send', message.value);
</script>

defineEmits() returns a function that we can trigger in the same concept with this.$emits. We will certainly need to use ref() to declare a reactive data message for this component, the usage.

defineEmits()會返回一個函數(shù),我們可以用與 this.$emits 相同的概念來觸發(fā)它。我們肯定需要使用 ref() 來為該組件聲明反應式數(shù)據(jù)消息。

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

相關文章

  • VUE實現(xiàn)自身整體組件銷毀的示例代碼

    VUE實現(xiàn)自身整體組件銷毀的示例代碼

    這篇文章主要介紹了VUE實現(xiàn)自身整體組件銷毀的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01
  • vue實現(xiàn)圖片預覽放大以及縮小問題

    vue實現(xiàn)圖片預覽放大以及縮小問題

    這篇文章主要介紹了vue實現(xiàn)圖片預覽放大以及縮小問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • vue3.x項目中,出現(xiàn)紅色波浪線問題及解決

    vue3.x項目中,出現(xiàn)紅色波浪線問題及解決

    這篇文章主要介紹了vue3.x項目中,出現(xiàn)紅色波浪線問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue實現(xiàn)列表無縫滾動效果

    vue實現(xiàn)列表無縫滾動效果

    這篇文章主要為大家詳細介紹了vue實現(xiàn)列表無縫滾動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Vue腳手架搭建及創(chuàng)建Vue項目流程的詳細教程

    Vue腳手架搭建及創(chuàng)建Vue項目流程的詳細教程

    Vue腳手架指的是vue-cli,它是一個快速構(gòu)建**單頁面應用程序(SPA)**環(huán)境配置的工具,cli是(command-line-interfac)命令行界面,下面這篇文章主要給大家介紹了關于Vue腳手架搭建及創(chuàng)建Vue項目流程的相關資料,需要的朋友可以參考下
    2022-09-09
  • vue3實現(xiàn)無縫滾動組件的示例代碼

    vue3實現(xiàn)無縫滾動組件的示例代碼

    在日常開發(fā)中,經(jīng)常遇到需要支持列表循環(huán)滾動展示,特別是在數(shù)據(jù)化大屏開發(fā)中,所以小編今天為大家介紹一下如何利用vue3實現(xiàn)一個無縫滾動組件吧
    2023-09-09
  • vue+node實現(xiàn)圖片上傳及預覽的示例方法

    vue+node實現(xiàn)圖片上傳及預覽的示例方法

    這篇文章主要介紹了vue+node實現(xiàn)圖片上傳及預覽的示例方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • 基于Vue 和 iView分片上傳功能實現(xiàn)(上傳組件)

    基于Vue 和 iView分片上傳功能實現(xiàn)(上傳組件)

    本文介紹了基于Vue和iView的文件分片上傳技術,通過將文件拆分成多個小塊并逐塊上傳,解決了大文件上傳時的諸多問題,如上傳速度慢、超時和網(wǎng)絡中斷等,它還展示了如何實現(xiàn)分片上傳的進度顯示、錯誤處理和斷點續(xù)傳等功能,感興趣的朋友跟隨小編一起看看吧
    2025-01-01
  • 如何解決this.$refs.form.validate()不執(zhí)行的問題

    如何解決this.$refs.form.validate()不執(zhí)行的問題

    這篇文章主要介紹了如何解決this.$refs.form.validate()不執(zhí)行的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Vue中實現(xiàn)權限控制的方法示例

    Vue中實現(xiàn)權限控制的方法示例

    這篇文章主要介紹了Vue中實現(xiàn)權限控制的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-06-06

最新評論

思茅市| 滁州市| 临海市| 新宁县| 金沙县| 武安市| 惠州市| 久治县| 华亭县| 宁强县| 桃江县| 临朐县| 沙洋县| 阜新市| 东台市| 凤冈县| 八宿县| 万源市| 田东县| 临西县| 黄浦区| 永川市| 岳普湖县| 洛隆县| 西林县| 河东区| 新丰县| 冕宁县| 随州市| 南部县| 灌南县| 富蕴县| 普格县| 鄂尔多斯市| 贵德县| 鹤庆县| 罗源县| 比如县| 诸暨市| 蓝山县| 兴文县|