希望明天越|

张尊娟

园龄:5年3个月粉丝:31关注:1

子组件调用父组件的方法

子组件调用父组件的方法

使用场景:

父组件中有个弹窗(子组件),需要结合父组件和子组件的数据作为参数去请求
调用父组件的方法关闭子组件等

如:父组件是输入提现或充值的金额,点击确定弹出子组件,弹窗是展示输入的金额和费率,发送验证码输入验证码,
输入验证码,点击确定的时候需要给父组件传验证码并且调用父组件的方法

1. this.$parent.event
2. this.$emit()
3. 父组件把方法传给子组件

this.$parent.event

this.$parent.getBill(data);

this.$emit()

//父组件
<child @childern = "fatherMethods"/>

//子组件
<button @click ="confirm">确认</button>

methods:{
    confirm(){
            this.$emit('childern')
    }
}

父组件把方法传给子组件

//父组件
<child :childern = "fatherMethods"/>
//子组件
<script>
export default{
    props:{
            childern:{
                type:Function,
                default:null,            
            }
    },
    methods:{
        if(this.childern){
            this.childern()
        }
    }
}

参考文章 https://www.php.cn/faq/483395.html

本文作者:张尊娟

本文链接:https://www.cnblogs.com/wszzj/p/17492731.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   张尊娟  阅读(296)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起