尝试在Vue中尝试Hello World时会出现错误

发布于 2025-02-07 10:14:15 字数 442 浏览 1 评论 0原文

我有这个错误

组件模板应完全包含一个根元素。如果是 在多个元素上使用v-if,使用v-else-if链接。

尝试时:

<template>
    <h1>Il tuo oroscopo</h1>
    <h3>{{ msg }}</h3>
</template>

<script>
export default {
    data() {
        return {
            msg: 'Hello World!'
        }
    }
}
</script>

<style scoped></style>

I got this error :

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.

When try :

<template>
    <h1>Il tuo oroscopo</h1>
    <h3>{{ msg }}</h3>
</template>

<script>
export default {
    data() {
        return {
            msg: 'Hello World!'
        }
    }
}
</script>

<style scoped></style>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音盲 2025-02-14 10:14:15

您必须将DIV放在根本上。只有一个。

<template>
<div>
    <h1>Il tuo oroscopo</h1>
    <h3>{{ msg }}</h3>
</div>
</template>

<script>
export default {
    data() {
        return {
            msg: 'Hello World!'
        }
    }
}
</script>

<style scoped></style>

You have to put a div in root. Only one.

<template>
<div>
    <h1>Il tuo oroscopo</h1>
    <h3>{{ msg }}</h3>
</div>
</template>

<script>
export default {
    data() {
        return {
            msg: 'Hello World!'
        }
    }
}
</script>

<style scoped></style>
錯遇了你 2025-02-14 10:14:15

正如错误所说的那样,组件模板应完全包含一个根元素。

这意味着,将模板内容包裹在单个元素中,该元素将作为根元素可用。因此,用&lt; div&gt;包裹将解决此错误。

演示

Vue.component('child', {
  props: ['childmsg'],
  template: `<div><h1>Il tuo oroscopo</h1>
                     <h3>{{ childmsg }}</h3></div>`
});

var app = new Vue({
  el: '#app',
  data: {
        msg: 'Hello World!'
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <child :childmsg="msg">
  </child>
</div>

As error says itself Component template should contain exactly one root element.

This means, wrapped the template content with in a single element which will be work as a root element. Hence, wrapping with a <div> will resolve this error.

Demo :

Vue.component('child', {
  props: ['childmsg'],
  template: `<div><h1>Il tuo oroscopo</h1>
                     <h3>{{ childmsg }}</h3></div>`
});

var app = new Vue({
  el: '#app',
  data: {
        msg: 'Hello World!'
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <child :childmsg="msg">
  </child>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文