unmount-innerchild-out-in-mode.vue 903 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <script setup vapor>
  2. import { defineVaporComponent, onUnmounted, ref, template } from 'vue'
  3. const calls = []
  4. window.getCalls = () => calls
  5. window.resetCalls = () => calls.splice(0, calls.length)
  6. const toggle = ref(true)
  7. const TrueBranch = defineVaporComponent({
  8. name: 'TrueBranch',
  9. setup() {
  10. onUnmounted(() => {
  11. calls.push('TrueBranch')
  12. })
  13. return template('<div>0</div>')()
  14. },
  15. })
  16. const includeRef = ref(['TrueBranch'])
  17. const click = () => {
  18. toggle.value = !toggle.value
  19. if (toggle.value) {
  20. includeRef.value = ['TrueBranch']
  21. } else {
  22. includeRef.value = []
  23. }
  24. }
  25. </script>
  26. <template>
  27. <div class="keep-alive">
  28. <div>
  29. <transition mode="out-in">
  30. <KeepAlive :include="includeRef">
  31. <TrueBranch v-if="toggle"></TrueBranch>
  32. </KeepAlive>
  33. </transition>
  34. </div>
  35. <button @click="click">button</button>
  36. </div>
  37. </template>