【问题标题】:VueJS error compiling templateVueJS错误编译模板
【发布时间】:2017-09-19 09:17:41
【问题描述】:

我刚刚使用 VueJS 和 Vue-loader 完成了我的第一个项目。

所以我做了我的第一个组件来显示一条简单的消息,当我发出一条消息时它工作正常,但是当我发出多条消息时我得到一个错误:

(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.

这是我的代码。我对此很陌生,我不知道出了什么问题。

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>

希望有人能帮忙!

【问题讨论】:

    标签: javascript webpack vue.js


    【解决方案1】:

    这个错误是不言自明的。每个组件中应该只有一个根元素。因此,只需将所有内容打包在一个 div 中即可。

    <template>
        <div>
            <message>This is a small message!</message>
            <message>Another one</message>
        </div>
    </template>
    

    【讨论】:

    • 啊当然!我在我的 javascript 中进行了更多搜索,我完全错过了它。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多