I have an ancestor Vue 2 component that uses provide/inject to inject a function from the ancestor to any nested (descendant) component. However I'm finding that inside that function this is not bound to the ancestor but to the descendant.
How can I get a reference the ancestor from inside the injected function onCallback() ?
Example:
<template>
<Intermediary />
</template>
<script>
export default {
name: "Ancestor",
props: ["val"],
provide: {
onCallback() {
console.log(this.val); // <<< fails as `this` is bound to caller component
}
}
}
</script>
<template>
<Descendant />
</template>
<script>
export default {
name: "Intermediary"
}
</script>
<template>
<button @click="onCallback">Click Me!</button>
</template>
<script>
export default {
name: "Descendant",
inject["onCallback"]
}
</script>