The template component must contain exactly one root element. If you use v-if for multiple elements, use v-else-if instead.
The right approach
<template> <div> <p></p> <p></p> </div> </template>
Wrong approach
<template> <p></p> <p></p> </template>
Multi-root components
The way to solve this problem is to use functional components, which are components in which you should not transmit any reactive data, which means that the component will not monitor any changes in the data, nor will it update itself when something changes in the parent component.
Since this is a workaround, it comes with a price, no lifecycle hooks are passed to the functional components, they are smaller than the instance, and you can no longer reference this , and everything is transferred with context.
Here's how you can create a simple functional component.
Vue.component('my-component', { // you must set functional as true functional: true, // Props are optional props: { // ... }, // To compensate for the lack of an instance, // we are now provided a 2nd context argument. render: function (createElement, context) { // ... } })
Now that we have examined functional components in detail, let's look at how to create multi-root components, for this I will give a general example.
<template> <ul> <NavBarRoutes :routes="persistentNavRoutes"/> <NavBarRoutes v-if="loggedIn" :routes="loggedInNavRoutes" /> <NavBarRoutes v-else :routes="loggedOutNavRoutes" /> </ul> </template>
Now, if we look at the NavBarRoutes template
<template> <li v-for="route in routes" :key="route.name" > <router-link :to="route"> {{ route.title }} </router-link> </li> </template>
We cannot do something like this, we will violate the restriction of one root component
Solution Make this component functional and use a render
{ functional: true, render(h, { props }) { return props.routes.map(route => <li key={route.name}> <router-link to={route}> {route.title} </router-link> </li> ) }
Here you have it you created a multi-root component, happy coding
Link for more information visit: https://blog.carbonteq.com/vuejs-create-multi-root-components/