Vue3 父组件调用子组件的方法


// 父组件

<template>
    <div>
     父页面 <son-com ref="sonRef"/> <button @click="handleClick">test</button> </div> </template> <script> import { defineComponent, ref, } from 'vue'; export default defineComponent({ setup(){
    const sonRef = ref(null);
      const handleClick = () => {
         sonRef.value.song();
      }
  return { sonRef, handleClick, } 
  }
})
</script>
// 子组件

<template>

        子页面
    </div>
</template>

<script>
import {
  defineComponent
} from 'vue';

export default defineComponent({
    setup(){
    const song = () => alert('hello world');
    return { 
      song, // 别忘记 return
    }
  }
})
</script>

 如果是TS定义可以使用

const sonRef = ref<null | HTMLElement>(null);

vue2调用子组件方法

vue2调用子组件方法

 

相关文章:

  • 2022-12-23
  • 2022-02-12
  • 2021-08-19
  • 2021-04-16
猜你喜欢
  • 2022-03-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-23
相关资源
相似解决方案