v-if Conditional Rendering Directive

Last updated: 2026-10-22

v-if Conditional Rendering Directive

v-if renders a block only when the expression is truthy. When falsy the element is destroyed and removed from the DOM; when truthy again it is re-created.

GroupDirectives
KindDirective
ScopeTemplate
Vue Version3.0+
SSR SupportYes

📝 Syntax

<div v-if="condition">...</div>
<div v-else-if="anotherCondition">...</div>
<div v-else>...</div>

⚙️ Arguments

expressionAny expression evaluating to truthy/falsy (written directly, without braces)

Returns:Directive, no return value

▶ Example

Edit the code below and see the live result:

Output:

The page shows 'Now you see me'. Clicking the button removes the paragraph from the DOM.

💡 Key Points

💡 Tip Toggling is expensive (destroy/re-create DOM). For frequent toggling prefer v-show.
⚠️ Warning Never use v-if and v-for on the same element: v-if has higher priority and cannot access the loop variable. Nest a template instead.
ℹ️ Note v-else / v-else-if must directly follow the v-if element — nothing in between.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q What is the difference between v-if and v-show?
A v-if truly destroys/re-creates the element (lazy); v-show always renders it and only toggles CSS display.
Q Why is the element gone from DevTools when the condition is falsy?
A That is exactly v-if: the element is removed from the DOM and destroyed, not hidden.
Q Can v-if be combined with v-for?
A Not on the same element. Wrap the v-for element in an outer <template v-if> instead.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏