VUE中使用this.$emit()的使用进行父子组件之间传值

vue中使用this.$emit()的使用:

子组件child.vue

<template>
    <button @click=buttonClick>点击事件</button>
</template>

export default {
    name: 'ChildComponent',
    methods: {
        buttonClick() {
         // 触发点击事件,并传递数据给父组件
           this.$emit('parent-event','xiaoming')
        }

    }
}

当点击时,进入到buttonClick()方法,此时调用this.$emit()方法触发自定义事件parent-event

并传递了'xiaoming'作为数据

父组件parent.vue通过监听parent-event来接收这个自定义事件并执行

<template>
    <child @parent-event="handleParentEvent"></child>
</template>

// 在父组件中引入子组件
import childComponent  from '@/component/childComponent'

export default {
    name: 'Parent',
    data(){
        return {
        }
    },
// 注册子组件
   components: {
        childComponent
     },
   methods: {
        handleParentEvent(data) {
            // 处理自定义事件的逻辑
               console.log(data)// 输出'xiaoming'
        }
    }
}

参考文章:Vue中的this.$emit()方法详解_vue.js_脚本之家 (jb51.net)

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
this.$emitVue.js用于触发自定义事件并递数据给父组件的方法。在子组件使用this.$emit可以触发父级组件定义的$emit注册的事件,并递子组件的参数。例如,在子组件的方法使用this.$emit("delete",this.index)可以触发父组件定义的"delete"事件,并将this.index作为参数递给父组件。 在另一个例子,子组件的sendData方法使用this.$emit("myevent", this.childData)触发父组件定义的"myevent"事件,并将this.childData作为参数递给父组件。 子组件通过this.$emit向父组件的具体使用方法是在子组件使用this.$emit("function", param),其function是父组件定义的函数,param是需要递给父组件的参数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Vue入门:: this.$emit:Vue父子组件(子父之间的通信)和splice()方法的用法](https://blog.csdn.net/godot06/article/details/107960472)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【Vue】学习笔记-组件](https://blog.csdn.net/weixin_49202767/article/details/123142041)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Vue组件向父组件(this.$emit()方法)](https://blog.csdn.net/weixin_39390837/article/details/117471942)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值