Problems with v-if in vue

<div v-if="loginType ==="username"">
    <label >username</label>
    <input placeholder="enter your username" key="username-input">
  </div>
  <div v-else>
    <label>email</label>
    <input placeholder="enter your email" key="email-input">
  </div>

as soon as I finish writing the above code, vue-cli has the following error


./node_modules/vue-loader/lib/template-compiler?{"id":"data-v-f68cdee0","hasScoped":true,"transformToRequire":{"video":["src","poster"],"source":"src","img":"src","image":"xlink:href"},"buble":{"transforms":{}}}!./node_modules/vue-loader/lib/selector.js?type=template&index=0!./src/components/DemoIf.vue
(Emitted value instead of an instance of Error) 
  Error compiling template:
  
  <div>
  <div v-if="loginType ==="username"">
    <label >username</label>
    <input placeholder="enter your username" key="username-input">
  </div>
  <div v-else-if="loginType ==="email"">
    <label>email</label>
    <input placeholder="enter your email" key="email-input">
  </div>
  </div>
  <buttob @click="changed"></buttob>
  
  - Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead.

 @ ./src/components/DemoIf.vue 11:0-362
 @ ./src/router/index.js
 @ ./src/main.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main.js
Dec.23,2021

Component template should contain exactly one root element . If you are using v-if on multiple elements, use v-else-if to chain them instead. The component template should contain only one root element

<template> 
 <div>
    
 </div>
</template> 

as I said upstairs, there can only be one root element in template

<template> 
 <div v-if="loginType ==='username'">
    ....
 </div>
 <div v-else>
    ....
 </div>
</template> 
Change

to

<template> 
   <div>
     <div v-if="loginType ==='username'">
        ....
     </div>
     <div v-else>
        ....
     </div>
   </div>
</template> 

in vuejs, especially after vue2.0, the outermost layer of a component needs to be wrapped with a "root element", otherwise it will make an error, which is not found in this vue1.0. So for the use of v-if in vueJs, if you act directly on the "root element" of a component, you will report an error. It should be changed like this:

<template> 
 <div v-if="loginType ==='username'">
    ....
 </div>
 <div v-else>
    ....
 </div>
</template> 


<template> 
  <div>
     <div v-if="loginType ==='username'">
    ....
     </div>
  </div>

</template>

Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead.

this error message has been said. There can only be one root element in template

.

looking at your error message, I feel that it is not the problem of br.
is < buttob @ click= "changed" > < / buttob > is not written in the above div, resulting in two root elements of the page

.
Menu