vue子组件向父组件传值this.$emit用法

一、在父组件中

1.将子组件引入父组件

import RelationalGraph from "@/views/atlas/relationequip.vue"

 2.定义组件

components:{
    RelationalGraph,
},

3.引用子组件 

<RelationalGraph ref="RealtionalGraph"/>

二、子组件中 

4.封装一个方法并调用

方法名随意,子向父传值使用

this.$emit("eventname",parmas);

//1.在data中有建一个对象并赋值
//2.在methods中定义一个方法,并放入$emit
//3.在需要调用方法的地方调用方法,如mounted中或者按钮点击事件中
data:{
    return {
        FormData:{
            data1:'a',
            data2:'b',
},
        //FormData为需要传递的数据,可以是对象,数组,单个的值
    }
},

monunted(){
    this.sonToFather();
    //调用传递的方法名,注意加this
},

methods(){
    sonToFather(){
        this.$emit("getFormData",this.FormData);
        //前面为事件名,用于父组件调用,后面为要传递的值,注意加this
    }
}

三、回到父组件中

5.父组件中的引用子组件处添加函数

@eventname="function"

<RelationalGraph ref="RelationalGraph" @getFormData="receiveFormData"/>

 6.父组件的methods中写入function获取传过来的值

data:{
    return {
        fatherFormData:{},
    }
}
methods:{
    receiveFormData(val){
        this.fatherFormData=val;//val为形参,代表传过来的FormData,可以随便写
    }
}

注意:子传父过程中的事件名不是方法名,跟那个sonToFather这个单词没有任何关系

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值