B
B
BonBon Slick2017-03-27 15:16:43
JavaScript
BonBon Slick, 2017-03-27 15:16:43

Component template should contain exactly one root element Vue2?

<div id="app">
        <counter></counter>
    </div>

    <template id="custom_template">
        <h1>Heading</h1>
        <button type="submit">
            @{{ counter }}
        </button>
    </template>

    <script type="text/javascript">

       Vue.component('counter', {
        template: '#custom_template',

        data: function () {
            return {
                counter: 0
            }
        }
    });

       new Vue({
           el: "#app",
       });
   </script>


Will return an error:
[Vue warn]: Error compiling template:


        <h1>Heading</h1>
        <button type="submit">
            {{ counter }}
        </button>
    

- 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.
 
(found in <Counter>)


Why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
g00dv1n, 2017-03-27
@BonBonSlick

h1 and button are on the same level. Vue doesn't allow it. You need to wrap them in a container.

<template id="custom_template">
      <div>
          <h1>Heading</h1>
          <button type="submit">
              @{{ counter }}
          </button>
      </div>
</template>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question