show an example of a parent component using a value from a child componenent using defineexpose in vue 3 composition api in typescript

Here's an example:

ChildComponent.vue

<template>
  <div>
    {{ exposedValue }}
  </div>
</template>

<script lang="ts">
import { defineComponent, defineExpose, ref } from 'vue'
export default defineComponent({
  setup() {
    const exposedValue = ref('Hello, World!')
    defineExpose({ exposedValue })
    return {
      exposedValue
    }
  }
})
</script>
321 chars
19 lines

ParentComponent.vue

<template>
  <div>
    <ChildComponent ref="child" />
    {{ child.exposedValue }}
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import ChildComponent from '@/components/ChildComponent.vue'

export default defineComponent({
  components: { ChildComponent },
  setup() {
    const child = ref(null)
    return {
      child
    }
  }
})
</script>
384 chars
22 lines

Note that in the parent component, we are creating a ref named child and passing it to the ChildComponent using the ref attribute. Then, we are accessing the exposed value of the child component using child.exposedValue.

Also, make sure that you are using defineExpose in the child component to expose the value.

gistlibby LogSnag