vue3父组件使用ref获取子组件的属性和方法

在vue3中父组件访问子组件中的属性和方法是需要借助于ref:

1.<script setup> 中定义响应式变量 例如: const demo1 = ref(null)

2.在引入的子组件标签上绑定ref属性的值与定义的响应式变量同名( <demo1 ref="demo1"/>)。

父组件代码如下:

|----------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <template> ``<demo1 ref=``"demo1"``/> ``<demo2 ref=``"demo2"``/> ``<demo3 ref=``"demo3"``/> </template> <script setup> import Demo1 from ``'@/components/demo1.vue' import Demo2 from ``'@/components/demo2.vue' import Demo3 from ``'@/components/demo3.vue' import {ref,onMounted} from ``'vue' const demo1 = ref(``null``) const demo2 = ref(``null``) const demo3 = ref(``null``) onMounted(()=> { ``console.log(demo1.value.count,``'demo1子组件'``) ``console.log(demo2.value?.a,``'demo2子组件'``) ``console.log(demo3.value.list[0],``"demo3子组件"``) }) </script> |

子组件代码如下:

demo1.vue

|----------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <template> ``<h1>i``'m demo1 content{``{count}}</h1> </template> <script > import {ref} from '``vue' export ``default { ``setup () { ``const count = ref(999) ``return { ``count ``} ``} } |

此时父组件可以获取到子组件的count属性(此时子组件用的是 export default 的写法)

demo2

|-----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 | <template> ``<h1>我是demo2</h1> </template> <script setup> import {defineExpose,ref} from ``'vue' const a = ref(``'helloss'``) </script> |

当使用 <script setup> 写法会导致父组件无法访问到子组件中的属性和方法。

使用 <script setup> 的组件,想要让父组件访问到它的属性和方法需要借助与defineExpose来指定需要暴露给父组件的属性。

更改后的demo2组件

|-------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 | <template> ``<h1>我是demo2</h1> </template> <script setup> import {defineExpose,ref} from ``'vue' const a = ref(``'helloss'``) defineExpose({ ``a }) </script> |

demo3

|----------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <template> ``<h1>我是demo3</h1> </template> <script> export ``default { ``data () { ``return { ``list:[``'a'``,``'b'``,``'c'``] ``} ``}, ``methods: { ``btn () { ``} ``} } |

这种方式,父组件可以正常获取到里面的属性和方法。

相关推荐
星栈4 小时前
我在浏览器里试了试 PhotoCraft|Rust + WASM 修图,是玩具还是生产力?
前端
雨中飘荡的记忆5 小时前
淘宝搜不到的东西,能不能去全世界找?一个程序员的创业尝试
前端·设计
kyriewen5 小时前
我让 4 位 AI 面试官打分,同一个我差了 40 分
前端·面试·程序员
htzyl2066 小时前
Vue3中watch的deep默认值:reactive与ref为什么相反
前端·vue.js
用户112936112196 小时前
没写样式为什么还找我要 wxss?uni-app 这个 ENOENT 坑多数人误判了
前端·微信小程序·uni-app
用户553665935266 小时前
从零做一个全国山峰与线路库:山野抽签的初衷、架构与踩坑
前端
夕水7 小时前
用 SolidJS 复刻 DeepSeek Chat —— 顺便把 ew-solid-ui 拷打了一遍
前端·javascript
qq_274475157 小时前
第 17 篇:ES6 语法速览:解构赋值、展开运算符、模板字符串、对象简写
前端·ecmascript·es6
2603_965896627 小时前
JavaScript 原型与原型链,读懂 JS 对象的底层机制
开发语言·javascript·原型模式
凯程序猿1号8 小时前
Web墨卡托EPSG3857瓦片行列号XYZ与经纬度双向换算及地面分辨率推导
前端·电脑