I want to invoke a function in the parent component from its child component.
In my Vue project, I have designed a reusable component that can be used across multiple pages. After the function is executed in this component, I aim to call a specific function in its parent element.
To achieve this, I utilize this.$parent.getLocations()
, which effectively triggers the desired function.
However, since the component should be flexible and adaptable, the function name needs to be dynamic - for instance, it could be getPets()
or getLunch()
.
I pass the keyword 'Location'
through as a prop, enabling me to use it within the component. But when attempting expressions like
'${this.$parent}.get${this.type}s()}'
(where this.type
is 'Location') or this.$parent + ".get" + this.type +" s()
', the functions fail to execute properly.
How do I set `getLocations` as a variable to run the specified function?