Mechazawa / vue2-teleport

Teleport component for vue2 that works the same as vue3's teleport
Creative Commons Zero v1.0 Universal
30 stars 4 forks source link

v-if does not work on the slot #3

Open xandao-dev opened 9 months ago

xandao-dev commented 9 months ago

The example provided in the readme does not work because modalOpen starts false, in the Teleport mounted the nodes are settled, but it doesn't watch for node changes.

<template>
  <div>
      <button @click="modalOpen = true">
          Open full-screen modal! (With teleport!)
      </button>

      <Teleport to="body">
        <div v-if="modalOpen" class="modal">
          <div>
            I'm a teleported modal! 
            (My parent is "body")
            <button @click="modalOpen = false">
              Close
            </button>
          </div>
        </div>
      </Teleport>
  </div>
</template>

Workaround: Make sure to have an initial node.

<template>
  <div>
      <button @click="modalOpen = true">
          Open full-screen modal! (With teleport!)
      </button>

      <Teleport to="body">
        <div v-show="modalOpen" class="modal">
          <div v-if="modalOpen">
            I'm a teleported modal! 
            (My parent is "body")
            <button @click="modalOpen = false">
              Close
            </button>
          </div>
        </div>
      </Teleport>
  </div>
</template>

Long-term solution: Watch for node changes in the Teleport component.

Mechazawa commented 4 months ago

Didn't consider that when I built it. I'll have a look at it

jackiotyu commented 4 months ago

2

7