Vue Tip: Avoid Empty Wrapper for Conditions

Michael Hoffmann
Jul 14, 2022
| 1 min read
| 119 views
Michael Hoffmann
Jul 14, 2022
1 min read
| 119 views
Template

There are many situations when you need to conditionally display multiple Vue components. To avoid "empty" wrappers you should use the <template>
instead of other HTML Tags. <template>
will not generate any HTML tag and serves as an invisible wrapper. The final rendered result will not include the <template>
element:
Bad
<template> <div v-if="loggedIn"> <ProfileImage /> <ProfileData /> </div></template>
Good
<template> <template v-if="loggedIn"> <ProfileImage /> <ProfileData /> </template></template>
v-else
and v-else-if
can also be used on <template>
.
If you liked this Vue tip, follow me on Twitter to get notified about new tips, blog posts, and more. Alternatively (or additionally), you can subscribe to my weekly Vue newsletter.
Show comments