zoukankan      html  css  js  c++  java
  • vue 子组件调用父组件方法

    有三种方法:

    1. this.$parent.fatherMethod();直接通过$parent调用父组件方法。
    2. 子组件用$emit向父组件出发一个事件,父组件监听该事件。
         childMethod() {
         // 子组件
           this.$emit('fatherMethod');
        }
      
        // 父组件
        <child @fatherMethod="fatherMethod"></child>
      
    3. 父组件将方法传入子组件,子组件调用即可。
         childMethod() {
         // 子组件
           this.$emit('fatherMethod');
        }
      
        // 父组件
        <child :fatherMethod="fatherMethod"></child>
      
        props: {
          fatherMethod: {
            type: Function,
            default: null
          }
        },
        methods: {
          childMethod() {
            if (this.fatherMethod) {
              this.fatherMethod();
            }
          }
        }
      
    有什么不同见解可以在评论区共同讨论
  • 相关阅读:
    html表单
    html基础
    MySQL数据库 数据的更新
    JAVA数据库编程
    集合框架
    线程和进程
    反射
    centos 7环境
    js中的this
    javascript的作用域以及闭包现象
  • 原文地址:https://www.cnblogs.com/lambertlt/p/14871446.html
Copyright © 2011-2022 走看看