vue3 setup中父组件通过Ref调用子组件的方法(实例代码)
作者:虎落鹰背 时间:2024-05-29 22:49:15
在setup()钩子函数中调用
父组件
<template>
<div>
我是父组件
<children ref="childrenRef" />
<button @click="handleChildren">触发子组件</button>
</div>
</template>
<script lang="ts">
import { ref, defineComponent } from 'vue'
import Children from './components/Children.vue';
export default defineComponent({
components: { Children }
setup() {
// ref的泛型除了指定any外 还可以指定<InstanceType<typeof Children>>
const childrenRef = ref<any>();
const handleChildren = () => childrenRef.value.isChildren();
return {
childrenRef,
handleChildren
}
},
})
</script>
子组件:
<template>
<div>
我是子组件
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
setup() {
const isChildren = () => {
console.log("我是子组件");
}
return {
isChildren,
}
}
})
</script>
如果是在setup()
钩子函数中使用,父组件通过ref
获取到子组件实例后,直接.value.函数名
即可调用子组件所定义的函数。其中ref的泛型可以指定any
和InstanceType<typeof Children>
在<srcipt setup>中调用
父组件
<template>
<div>
我是子组件
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
setup() {
const isChildren = () => {
console.log("我是子组件");
}
return {
isChildren,
}
}
})
</script>
子组件
<template>
<div>
我是子组件
</div>
</template>
<script setup lang="ts">
import { defineExpose } from 'vue';
const isChildren = () => {
console.log("我是子组件的第一个方法");
}
const isChildren2 = () => {
console.log("我是子组件的第二个方法");
}
defineExpose({ isChildren, isChildren2 })
</script>
在<srcipt setup>
中调用和setup()
钩子函数中调用不同的是:子组件中要通过defineExpose
将方法暴露给父组件。
??官网说明地址
来源:https://blog.csdn.net/qq_60361946/article/details/126333011
标签:vue3,setup,父组件,子组件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入了解Go的interface{}底层原理实现
2024-05-21 10:19:03
![](https://img.aspxhome.com/file/2023/3/125073_0s.jpg)
让innerText在firefox火狐和IE浏览器都能用的写法
2024-05-02 16:17:24
Pytorch 图像变换函数集合小结
2022-06-14 08:52:09
![](https://img.aspxhome.com/file/2023/9/84929_0s.jpg)
利用Python实现绘制论文中的曲线图
2022-12-14 12:04:37
![](https://img.aspxhome.com/file/2023/9/78489_0s.png)
兼容主流浏览器,纯CSS下拉菜单
2010-09-05 20:30:00
python中defaultdict字典功能特性介绍
2022-06-07 23:27:45
![](https://img.aspxhome.com/file/2023/7/99037_0s.png)
详解如何在Javascript中使用Object.freeze()
2024-04-10 16:10:17
![](https://img.aspxhome.com/file/2023/4/136784_0s.png)
Python 中 f-Strings 的作用
2022-12-04 11:44:55
![](https://img.aspxhome.com/file/2023/3/81803_0s.jpg)
Python设计足球联赛赛程表程序的思路与简单实现示例
2023-05-02 20:58:05
![](https://img.aspxhome.com/file/2023/3/89333_0s.png)
Python爬虫之urllib基础用法教程
2023-08-09 08:29:01
MySQL查询倒数第二条记录实现方法
2024-01-26 07:15:50
python机器学习之随机森林(七)
2023-10-20 00:25:13
pytorch中的weight-initilzation用法
2022-03-22 23:28:24
mssql server 数据库附加不上解决办法分享
2011-09-30 11:55:20
Python xlrd excel文件操作代码实例
2021-05-19 21:52:18
![](https://img.aspxhome.com/file/2023/3/107953_0s.png)
asp(JavaScript)自动判断网页编码并转换的代码
2011-03-03 11:19:00
python的PyPDF2实现pdf文件切割和合并
2023-02-07 19:57:39
![](https://img.aspxhome.com/file/2023/8/102968_0s.png)
Vue中$router与 $route的区别详解
2024-04-30 10:38:42
![](https://img.aspxhome.com/file/2023/7/130127_0s.png)
Python numpy和matlab的几点差异介绍
2023-12-14 23:26:39
MySQL的root帐户密码重置方法
2007-08-24 15:53:00