If not using script setup
it is directly callable at the parent component by
this.$refs.childComponent.methodToInvoke();
Otherwise if a script setup
section must/should be declared at the child component, I learned that I had to declare it like this inside a defineComponent
section.
<script>
import {defineComponent, defineExpose} from 'vue';
export default defineComponent({
setup() {
// context now contains this
defineExpose({
methodToInvoke: this.methodToInvoke
});
},
methods: {
methodToInvoke(): {
...
}
}
});
</script>
It cannot be declared as a separate section in a SFC (single file component) when having the component also declared in a <script>
section
<script setup>
import {defineExpose} from 'vue';
defineExpose({
methodToInvoke: this.methodToInvoke
});
</script>
since the this
context is not given here.
If I got something wrong please let me know since I am not a vue3 specialist and rather just beginning with vue3 ;-)