I am currently exploring how to access $refs in Vue 3 using the Composition API. In my template, I have two child components and I specifically need to obtain a reference to one of them:
<template>
<comp-foo />
<comp-bar ref="table"/>
</template>
When working with Template Refs, the ref attribute is crucial as it allows us to directly access a specific DOM element or child component instance once it has been mounted.
In my code, everything works perfectly fine when utilizing the Options API:
mounted() {
console.log("Mounted - ok");
console.log(this.$refs.table.temp());
}
However, when attempting to achieve the same result using the Composition API, I encounter an error:
setup() {
const that: any = getCurrentInstance();
onMounted(() => {
console.log("Mounted - ok");
console.log(that.$refs.table.temp());//ERROR that.$refs is undefined
});
return {};
}
If anyone can provide guidance on how to achieve this using the Composition API, it would be greatly appreciated.