Skip to content
Advertisement

Watch child properties from parent component in vue 3

I’m wondering how I can observe child properties from the parent component in Vue 3 using the composition api (I’m working with the experimental script setup).

JavaScript
JavaScript

I want to understand how I can watch changes in the child component from the parent component. My not working solution is inspired by the Vue.js 2 Solution asked here. So I don’t want to emit the count.value but just watch for changes.

Thank you!

Advertisement

Answer

The Bindings inside of <script setup> are “closed by default” as you can see here.

However you can explicitly expose certain refs. For that you use useContext().expose({ ref1,ref2,ref3 })

So simply add this to Child.vue:

JavaScript

and then change the Watcher in Parent.vue to:

JavaScript

And it works!

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement