Is it possible to provide a function in Vue 3 and then call the function in the child component?
I know that this worked with the option API:
provide() {
return {
$list: this,
};
},
But how can I achieve the same with the composition API?
Here is my approach:
Parent component:
setup(props) {
const handleEdit = (item) => {
emit("edit", item);
};
provide("$list", handleEdit);
return { handleEdit };
}
Child component:
setup(props) {
const { item } = props;
const list = inject("$list");
const handleEditItem = (e) => {
list.handleEdit(item);
};
}
And here the error
Uncaught TypeError: _ctx.handleEditItem is not a function