inject-composition.uvue 1.1 KB
Newer Older
1 2 3 4 5 6 7
<template>
  <view>
    <text>inject page</text>
    <text class="mt-10 msg">msg: {{msg}}</text>
    <text class="mt-10 num">num: {{num}}</text>
    <text class="mt-10 obj">obj: {{JSON.stringify(obj)}}</text>
    <text class="mt-10 arr">arr: {{JSON.stringify(arr)}}</text>
8 9
    <text class="mt-10 arr-0">arr[0]: {{ arr[0]}}</text>
    <text class="mt-10 fn">fn: {{fn?.()}}</text>
10 11 12 13 14 15 16 17
    <text class="mt-10 has-injection-context">hasInjectionContext:
      {{checkHasInjectionContextRes}}</text>
    <button class="mt-10 check-has-injection-context-btn" @click="checkHasInjectionContext">check
      hasInjectionContext</button>
  </view>
</template>

<script setup lang='uts'>
18
  const msg = inject<string>('msg')
19 20
  const num = inject('num')
  const obj = inject('obj')
21 22
  const arr = inject<number[]>('arr', [99])
  const fn = inject<() => string>('fn')
23 24 25 26 27 28 29 30

  const checkHasInjectionContextRes = ref('')

  const checkHasInjectionContext = () => {
    checkHasInjectionContextRes.value = `${hasInjectionContext()}`
  }
  checkHasInjectionContext()
</script>