v-show on a Vue element?v-show. What is the visible effect when the condition is false?<template> <div v-show="isVisible">Hello!</div> </template> <script setup> import { ref } from 'vue' const isVisible = ref(false) </script>
v-show toggles the CSS display property. The element stays in the DOM but is hidden visually using display: none. This means it is still present but not visible.
v-if on a Vue element?v-if. What happens to the element when the condition is false?<template> <div v-if="isVisible">Hello!</div> </template> <script setup> import { ref } from 'vue' const isVisible = ref(false) </script>
v-if conditionally renders the element. When the condition is false, the element is completely removed from the DOM. It does not exist until the condition becomes true again.
mounted lifecycle hook called when toggling v-if?v-if to toggle visibility, how many times is the child's mounted hook called if the condition toggles from false to true and back to false and then true again?<template> <ChildComponent v-if="show" /> <button @click="show = !show">Toggle</button> </template> <script setup> import { ref } from 'vue' import ChildComponent from './ChildComponent.vue' const show = ref(false) </script>
v-if removes them.When v-if removes a component, it is destroyed. When the condition becomes true again, the component is created and mounted anew. So toggling twice to true mounts twice.
mounted lifecycle hook called when toggling v-show?v-show to toggle visibility, how many times is the child's mounted hook called if the condition toggles from false to true and back to false and then true again?<template> <ChildComponent v-show="show" /> <button @click="show = !show">Toggle</button> </template> <script setup> import { ref } from 'vue' import ChildComponent from './ChildComponent.vue' const show = ref(false) </script>
v-show keeps the component in the DOM and mounted. It only toggles CSS visibility. So the mounted hook runs once when the component first appears.
v-if and v-show in Vue 3?v-if and v-show to show the element only when isVisible is true?Option A correctly uses v-if and v-show with the same condition isVisible. The other options mix conditions or swap directives incorrectly.