parent-composition.uvue 676 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
<template>
  <view class="page">
    <child ref="childRef" />
  </view>
</template>

<script setup lang="uts">
  import Child from './child-composition.uvue'

  const childRef = ref<ComponentPublicInstance | null>(null)
  const str = ref('parent str')
  const num = ref(1)

  const getNum = () : number => { return num.value }

  const instance = getCurrentInstance()!.proxy!

  const callMethodByChild = () : number => {
    const childComponent = instance.$refs['childRef'] as ComponentPublicInstance
    return childComponent.$parent!.$callMethod('getNum') as number
  }

  defineExpose({
    str,
    getNum,
    callMethodByChild
  })
</script>