11

I just made my first project with VueJS and Vue-loader.

So I made my first component to show a simple message, it works fine when I make one message, but I get an error when I make multiple messages:

(Emitted value instead of an instance of Error) 
  Error compiling template:

  <message>This is a small message!</message>

  <message>Another one</message>

  - 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 is my code. I'm very new to this and I can't figure out what's wrong.

App.vue

<template>
    <message>This is a small message!</message>

    <message>Another one</message>
</template>

<script>
    import Message from './Components/Message.vue';

    export default {
        name: 'app',

        components: {
            Message,
        },

        data () {
            return {

            }
        }
    }
</script>

Message.Vue

<template>
    <div class="box">
        <p>
            <slot></slot>
        </p>
    </div>
</template>

<script>
    export default {

    }
</script>

<style>
    .box { background-color: #e3e3e3; padding: 10px; border: 1px solid #c5c5c5; margin-bottom: 1em;}
</style>

I hope somebody can help!

1 Answer 1

19

The error is pretty self-explanatory. You should have only one root element in each component. So just pack everything in a div.

<template>
    <div>
        <message>This is a small message!</message>
        <message>Another one</message>
    </div>
</template>
Sign up to request clarification or add additional context in comments.

1 Comment

Ah of course! I was more searching inside my javascript, I completely missed it. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.