In my VueJS 2 project, there are a parent component and a child component. The parent component sends a property named items
to the child component.
Whenever the user clicks on a button within the child component, it emits a refresh
event using this syntax:
$emit('refresh', category.id)
My goal is to catch this event in the parent component and, upon receiving it, call a function such as alert()
.
What I've gathered so far from the VueJS documentation is that you can use the v-on
directive to listen for events on elements like buttons. However, my parent component does not have any button element I can attach this listener to.
To clarify my approach, here's what I'm envisioning:
- The parent component loads and executes a
getData()
function, passing its result as a prop to the child component. - The user interacts with a button inside the child component.
- This action triggers an event within the child component.
- Subsequently, the parent component once again invokes the
getData()
function and updates the prop being sent to the child component.