current position:Home>In vue, the child component and the parent component method call each other

In vue, the child component and the parent component method call each other

2022-09-23 06:22:31zch981964

  1. 父组件调用子组件方法.

方法1:通过ref直接调用子组件的方法

//父组件中
<template>
    <div>
        <Button @click="handleClick">点击调用子组件方法</Button>
        <Child ref="child"/>
    </div>
</template>    
 
<script>
import Child from './child';
export default {
    
    methods: {
    
        handleClick() {
    
              this.$refs.child.sing();
        },
    },
}
</script>

//子组件中
<template>
  <div>我是子组件</div>
</template>
<script>
export default {
    
  methods: {
    
    sing() {
    
      console.log('我是子组件的方法');
    },
  },
};
</script>

方法2:通过组件的$emit、$on方法;

//父组件中
<template>
    <div>
        <Button @click="handleClick">点击调用子组件方法</Button>
        <Child ref="child"/>
    </div>
</template>    
<script>
import Child from './child';
export default {
    
    methods: {
    
        handleClick() {
    
               this.$refs.child.$emit("childmethod")    //子组件$on中的名字
        },
    },
}
</script>

//子组件中
<template>
    <div>我是子组件</div>
</template>
<script>
export default {
    
    mounted() {
    
        this.$nextTick(function() {
    
            this.$on('childmethods', function() {
    
                console.log('我是子组件方法');
            });
        });
     },
};
</script>
  1. 子组件调用父组件中的方法;

方法1:直接在子组件中通过this.$parent.event来调用父组件的方法

//父组件
<template>
  <p>
    <child></child>
  </p>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    
    components: {
    
      child
    },
    methods: {
    
      fatherMethod() {
    
        console.log('测试');
      }
    }
  };
</script>


//子组件
<template>
  <p>
    <button @click="childMethod()">点击</button>
  </p>
</template>
<script>
  export default {
    
    methods: {
    
      childMethod() {
    
        this.$parent.fatherMethod();
      }
    }
  };
</script>

方法2:在子组件里用$emit向父组件触发一个事件,父组件监听这个事件就行了

//父组件
<template>
  <p>
    <child @fatherMethod="fatherMethod"></child>
  </p>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    
    components: {
    
      child
    },
    methods: {
    
      fatherMethod() {
    
        console.log('测试');
      }
    }
  };
</script>

//子组件
<template>
  <p>
    <button @click="childMethod()">点击</button>
  </p>
</template>
<script>
  export default {
    
    methods: {
    
      childMethod() {
    
        this.$emit('fatherMethod');
      }
    }
  };
</script>

方法3:父组件把方法传入子组件中,在子组件里直接调用这个方法

//父组件
<template>
  <p>
    <child :fatherMethod="fatherMethod"></child>
  </p>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    
    components: {
    
      child
    },
    methods: {
    
      fatherMethod() {
    
        console.log('测试');
      }
    }
  };
</script>

//子组件
<template>
  <p>
    <button @click="childMethod()">点击</button>
  </p>
</template>
<script>
  export default {
    
    props: {
    
      fatherMethod: {
    
        type: Function,
        default: null
      }
    },
    methods: {
    
      childMethod() {
    
        if (this.fatherMethod) {
    
          this.fatherMethod();
        }
      }
    }
  };
</script>

copyright notice
author[zch981964],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/266/202209230605501966.html

Random recommended