0


vue3 调用子组件的方法

child.vue

<template></template><scriptsetup>constDoSomeThing=()=>{alert('点击事件');}// 输出组件的方法,让外部组件可以调用defineExpose({
    DoSomeThing,})</script><stylelang="less"scoped></style>

parent.vue

<template><childVueref="child"/><a-buttontype="primary"@click="ChildEvent">调用子组件的方法</a-button></template><scriptsetup>// setup 模式下,直接引用 Vue 文件,则相当于在组件中,注册了子组件import{ onMounted, ref }from'vue';import childVue from'./child.vue';// 创建于子组件同名的 变量名,则相当于获取了这个组件的实例// 注意 : setup 模式下,不要让 子组件上的 ref 值 与创建的变量值重名😓const child =ref();// 子组件实例只有 在组件被挂载之后才能被调用onMounted(()=>{
    console.log(child.value);})constChildEvent=()=>{
    child.value.DoSomeThing();}</script><stylelang="less"scoped></style>

本文转载自: https://blog.csdn.net/qq_38946996/article/details/127988861
版权归原作者 一只敲码的猫~ 所有, 如有侵权,请联系我们删除。

“vue3 调用子组件的方法”的评论:

还没有评论