Skip to content

Commit 52d8c4b

Browse files
docs: add options-api for Transitions with the Key example (#2708)
1 parent 6505ade commit 52d8c4b

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/guide/built-ins/transition.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,8 @@ Sometimes you need to force the re-render of a DOM element in order for a transi
587587

588588
Take this counter component for example.
589589

590+
<div class="composition-api">
591+
590592
```vue
591593
<script setup>
592594
import { ref } from 'vue';
@@ -602,10 +604,51 @@ setInterval(() => count.value++, 1000);
602604
</template>
603605
```
604606

607+
</div>
608+
<div class="options-api">
609+
610+
```vue
611+
<script>
612+
export default {
613+
data() {
614+
return {
615+
count: 1,
616+
interval: null
617+
}
618+
},
619+
mounted() {
620+
this.interval = setInterval(() => {
621+
this.count++;
622+
}, 1000)
623+
},
624+
beforeDestroy() {
625+
clearInterval(this.interval)
626+
}
627+
}
628+
</script>
629+
630+
<template>
631+
<Transition>
632+
<span :key="count">{{ count }}</span>
633+
</Transition>
634+
</template>
635+
```
636+
637+
</div>
638+
605639
If we had excluded the `key` attribute, only the text node would be updated and thus no transition would occur. However, with the `key` attribute in place, Vue knows to create a new `span` element whenever `count` changes and thus the `Transition` component has 2 different elements to transition between.
606640

641+
<div class="composition-api">
642+
607643
[Try it in the Playground](https://play.vuejs.org/#eNp9UsFu2zAM/RVCl6Zo4nhYd/GcAtvQQ3fYhq1HXTSFydTKkiDJbjLD/z5KMrKgLXoTHx/5+CiO7JNz1dAja1gbpFcuQsDYuxtuVOesjzCCxx1MsPO2gwuiXnzkhhtpTYggbW8ibBJlUV/mBJXfmYh+EHqxuITNDYzcQGFWBPZ4dUXEaQnv6jrXtOuiTJoUROycFhEpAmi3agCpRQgbzp68cA49ZyV174UJKiprckxIcMJA84hHImc9oo7jPOQ0kQ4RSvH6WXW7JiV6teszfQpDPGqEIK3DLSGpQbazsyaugvqLDVx77JIhbqp5wsxwtrRvPFI7NWDhEGtYYVrQSsgELzOiUQw4I2Vh8TRgA9YJqeIR6upDABQh9TpTAPE7WN3HlxLp084Foi3N54YN1KWEVpOMkkO2ZJHsmp3aVw/BGjqMXJE22jml0X93STRw1pReKSe0tk9fMxZ9nzwVXP5B+fgK/hAOCePsh8dAt4KcnXJR+D3S16X07a9veKD3KdnZba+J/UbyJ+Zl0IyF9rk3Wxr7jJenvcvnrcz+PtweItKuZ1Np0MScMp8zOvkvb1j/P+776jrX0UbZ9A+fYSTP)
608644

645+
</div>
646+
<div class="options-api">
647+
648+
[Try it in the Playground](https://play.vuejs.org/#eNp9U8tu2zAQ/JUFTwkSyw6aXlQ7QB85pIe2aHPUhZHWDhOKJMiVYtfwv3dJSpbbBgEMWJydndkdUXvx0bmi71CUYhlqrxzdVAa3znqCBtey0wT7ygA0kuTZeX4G8EidN+MJoLadoRKuLkdAGULfS12C6bSGDB/i3yFx2tiAzaRIjyoUYxesICDdDaczZq1uJrNETY4XFx8G5Uu4WiwW55PBA66txy8YyNvdZFNrlP4o/Jdpbq4M/5bzYxZ8IGydloR8Alg2qmcVGcKqEi9eOoe+EqnExXsvTVCkrBkQxoKTBspn3HFDmprp+32ODA4H9mLCKDD/R2E5Zz9+Ws5PpuBjoJ1GCLV12DASJdKGa2toFtRvLOHaY8vx8DrFMGdiOJvlS48sp3rMHGb1M4xRzGQdYU6REY6rxwHJGdJxwBKsk7WiHSyK9wFQhqh14gDyIVjd0f8Wa2/bUwOyWXwQLGGRWzicuChvKC4F8bpmrTbFU7CGL2zqiJm2Tmn03100DZUox5ddCam1ffmaMPJd3Cnj9SPWz6/gT2EbsUr88Bj4VmAljjWSfoP88mL59tc33PLzsdjaptPMfqP4E1MYPGOmfepMw2Of8NK0d238+JTZ3IfbLSFnPSwVB53udyX4q/38xurTuO+K6/Fqi8MffqhR/A==)
649+
650+
</div>
651+
609652
---
610653

611654
**Related**

0 commit comments

Comments
 (0)