Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS error compiling template

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!

like image 686
Nieck Avatar asked Apr 22 '17 11:04

Nieck


1 Answers

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>
like image 191
FitzFish Avatar answered Oct 21 '22 12:10

FitzFish