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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 CompA = defineVaporComponent({
  13. name: 'CompA',
  14. setup() {
  15. return template('<div>CompA</div>')()
  16. },
  17. })
  18. const CompB = defineVaporComponent({
  19. name: 'CompB',
  20. setup() {
  21. return template('<div>CompB</div>')()
  22. },
  23. })
  24. const CompC = defineVaporComponent({
  25. name: 'CompC',
  26. setup() {
  27. onUnmounted(() => {
  28. calls.push('CompC unmounted')
  29. })
  30. return template('<div>CompC</div>')()
  31. },
  32. })
  33. const includeToChange = ref(['CompA', 'CompB', 'CompC'])
  34. const currentView = shallowRef(CompA)
  35. const switchToB = () => (currentView.value = CompB)
  36. const switchToC = () => (currentView.value = CompC)
  37. const switchToA = () => {
  38. currentView.value = CompA
  39. includeToChange.value = ['CompA']
  40. }
  41. </script>
  42. <template>
  43. <div class="keep-alive-update-include">
  44. <div>
  45. <transition mode="out-in">
  46. <KeepAlive :include="includeToChange">
  47. <component :is="currentView" />
  48. </KeepAlive>
  49. </transition>
  50. </div>
  51. <button id="switchToB" @click="switchToB">switchToB</button>
  52. <button id="switchToC" @click="switchToC">switchToC</button>
  53. <button id="switchToA" @click="switchToA">switchToA</button>
  54. </div>
  55. </template>