switch-and-update-child-then-update-include-out-in-mode.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <script setup vapor>
  2. import {
  3. defineVaporComponent,
  4. onUnmounted,
  5. ref,
  6. shallowRef,
  7. template,
  8. } from 'vue'
  9. const calls = []
  10. window.getCalls = () => calls
  11. window.resetCalls = () => calls.splice(0, calls.length)
  12. const CompA2 = defineVaporComponent({
  13. name: 'CompA2',
  14. setup() {
  15. return template('<div>CompA2</div>')()
  16. },
  17. })
  18. const CompB2 = defineVaporComponent({
  19. name: 'CompB2',
  20. setup() {
  21. onUnmounted(() => {
  22. calls.push('CompB2 unmounted')
  23. })
  24. return template('<div>CompB2</div>')()
  25. },
  26. })
  27. const includeRef2 = ref(['CompA2'])
  28. const currentView2 = shallowRef(CompA2)
  29. const switchToB2 = () => {
  30. currentView2.value = CompB2
  31. includeRef2.value = ['CompA2', 'CompB2']
  32. }
  33. const switchToA2 = () => {
  34. currentView2.value = CompA2
  35. includeRef2.value = ['CompA2']
  36. }
  37. </script>
  38. <template>
  39. <div class="keep-alive-switch-then-update-include">
  40. <div>
  41. <transition name="test-anim" mode="out-in">
  42. <KeepAlive :include="includeRef2">
  43. <component :is="currentView2" />
  44. </KeepAlive>
  45. </transition>
  46. </div>
  47. <button id="switchToA" @click="switchToA2">switchToA</button>
  48. <button id="switchToB" @click="switchToB2">switchToB</button>
  49. </div>
  50. </template>