ChuckTest / Vue3-Basics

Tutorial for Vue 3
https://www.youtube.com/watch?v=FXpIoQ_rT_c
0 stars 0 forks source link

What is better in vue.js 2, use v-if or v-show? #1

Open chucklu opened 2 years ago

chucklu commented 2 years ago

What is better in vue.js 2, use v-if or v-show?

tl;dr

Assuming the question is strictly about performance:

v-show: expensive initial load, cheap toggling,
v-if: cheap initial load, expensive toggling.

Evan You provided a more in depth answer at VueJS Forum

v-show always compiles and renders everything - it simply adds the "display: none" style to the element. It has a higher initial load cost, but toggling is very cheap.

Incomparison, v-if is truely conditional: it is lazy, so if its initial condition is false, it won't even do anything. This can be good for initial load time. When the condition is true, v-if will then compile and render its content. Toggling a v-if block actually tearsdown everything inside it, e.g. Components inside v-if are acually destroyed and re-created when toggled, so toggling a huge v-if block can be more expensive than v-show.

chucklu commented 2 years ago

most cases you want to use v-if, only use v-show for toggle requirement