What's the difference between v-if and v-show in Vue.js?
V-if vs. V-Show
What is the difference between v-if and v-show and when should you use which?
Well, quite simply, v-if determines whether or not to render the element AT ALL, while v-show simply hides it from view setting "display" to "none."
When you use v-if, the rendering of that element is destroyed, which means the listeners and components nested within are also destroyed. This saves memory and improves your apps performance. They can always be recreated later, too.
V-if is also considered lazy. That means it only loads at the moment the conditional in the if block is finally determined to be true.
However, sometimes you don't want to destroy your components or elements and want to keep the listeners and child components active. In that case, you'd probably want to stick with "v-show."
Unlike "v-if," "v-show" elements are rendered at the same time as everything else.
So when should you use v-if or v-show.
Use "v-show" if:
- You plan on toggle that element often, since it has lower toggling costs
- You need to listen to changes on element while it is hidden
- If you want it to render along side the rest of your app
Use "v-if" if:
- If you don't plan on toggling the element often
- If you don't plan on listening to the hidden element and don't need access to its children in the DOM
- You want to lazy-load the component/element
Comments
Post a Comment