美文网首页
子组件调用父组件方法

子组件调用父组件方法

作者: 小话梅噢 | 来源:发表于2019-12-03 10:13 被阅读0次

1、直接在子组件中使用

this.$parent.fatherMethod();

其中fatherMethod是方法名

2、$emit

在父组件中需要对子组件进行方法注册(好比prop传参,如下写)

@fatherMethod="fatherMethod"

(其中前者是注册的名字,在子组件中使用,后者是在父组件中的方法名)

在子组件中直接使用(子组件中使用的名字要与注册时前者的名字相同)

this.$emit('fatherMethod');

3、使用prop传参,将方法直接传入

父组件中

:fatherMethod="fatherMethod"

子组件中先接收,后使用

props: {

      fatherMethod: {

        type: Function,

        default: null

      }

    },

this.fatherMethod();

相关文章

网友评论

      本文标题:子组件调用父组件方法

      本文链接:https://www.haomeiwen.com/subject/rgtcgctx.html