Vue js error: Component template should contain exactly one root element

后端 未结 8 2048
予麋鹿
予麋鹿 2020-11-29 04:37

I don\'t know what the error is, so far I am testing through console log to check for changes after selecting a file (for uploading).

When I run $ npm run watc

相关标签:
8条回答
  • 2020-11-29 04:42

    instead of using this

    Vue.component('tabs', {
        template: `
            <div class="tabs">
              <ul>
                <li class="is-active"><a>Pictures</a></li>
                <li><a>Music</a></li>
                <li><a>Videos</a></li>
                <li><a>Documents</a></li>
              </ul>
            </div>
    
            <div class="tabs-content">
              <slot></slot>
            </div>
        `,
    });
    

    you should use

    
    Vue.component('tabs', {
        template: `
          <div>
            <div class="tabs">
              <ul>
                <li class="is-active"><a>Pictures</a></li>
                <li><a>Music</a></li>
                <li><a>Videos</a></li>
                <li><a>Documents</a></li>
              </ul>
            </div>
    
            <div class="tabs-content">
              <slot></slot>
            </div>
          </div>
        `,
    });
    
    
    0 讨论(0)
  • 2020-11-29 04:54

    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 right approach is

    <template>
      <div> <!-- The root -->
        <p></p> 
        <p></p>
      </div>
    </template>
    

    The wrong approach

    <template> <!-- No root Element -->
        <p></p> 
        <p></p>
    </template>
    

    Multi Root Components

    The way around to that problem is using functional components, they are components where you have to pass no reactive data means component will not be watching for any data changes as well as not updating it self when something in parent component changes.

    As this is a work around it comes with a price, functional components don't have any life cycle hooks passed to it, they are instance less as well you cannot refer to this anymore and everything is passed with context.

    Here is 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 covered functional components in some detail lets cover how to create multi root components, for that I am gonna present you with a generic example.

    <template>
     <ul>
         <NavBarRoutes :routes="persistentNavRoutes"/>
         <NavBarRoutes v-if="loggedIn" :routes="loggedInNavRoutes" />
         <NavBarRoutes v-else :routes="loggedOutNavRoutes" />
     </ul>
    </template>
    

    Now if we take a look at NavBarRoutes template

    <template>
     <li
     v-for="route in routes"
     :key="route.name"
     >
     <router-link :to="route">
     {{ route.title }}
     </router-link>
     </li>
    </template>
    

    We cant do some thing like this we will be violating single root component restriction

    Solution Make this component functional and use 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 have created a multi root component, Happy coding

    Reference for more details visit: https://blog.carbonteq.com/vuejs-create-multi-root-components/

    0 讨论(0)
  • 2020-11-29 04:56

    For a more complete answer: http://www.compulsivecoders.com/tech/vuejs-component-template-should-contain-exactly-one-root-element/

    But basically:

    • Currently, a VueJS template can contain only one root element (because of rendering issue)
    • In cases you really need to have two root elements because HTML structure does not allow you to create a wrapping parent element, you can use vue-fragment.

    To install it:

    npm install vue-fragment
    

    To use it:

    import Fragment from 'vue-fragment';
    Vue.use(Fragment.Plugin);
    
    // or
    
    import { Plugin } from 'vue-fragment';
    Vue.use(Plugin);
    

    Then, in your component:

    <template>
      <fragment>
        <tr class="hola">
          ...
        </tr>
        <tr class="hello">
          ...
        </tr>
      </fragment>
    </template>
    
    0 讨论(0)
  • 2020-11-29 04:58

    I was confused as I knew VueJS should only contain 1 root element and yet I was still getting this same "template syntax error Component template should contain exactly one root element..." error on an extremely simple component. Turns out I had just mispelled </template> as </tempate> and that was giving me this same error in a few files I copied and pasted. In summary, check your syntax for any mispellings in your component.

    0 讨论(0)
  • 2020-11-29 05:00

    Note This answer only applies to version 2.x of Vue. Version 3 has lifted this restriction.

    You have two root elements in your template.

    <div class="form-group">
      ...
    </div>
    <div class="col-md-6">
      ...
    </div>
    

    And you need one.

    <div>
        <div class="form-group">
          ...
        </div>
    
        <div class="col-md-6">
          ...
        </div>
    </div>
    

    Essentially in Vue you must have only one root element in your templates.

    0 讨论(0)
  • 2020-11-29 05:01

    You need to wrap all the html into one single element.

    <template>
       <div>
            <div class="form-group">
                <label for="avatar" class="control-label">Avatar</label>
                <input type="file" v-on:change="fileChange" id="avatar">
                <div class="help-block">
                    Help block here updated 4                                                                     
    0 讨论(0)
提交回复
热议问题