javascript - - 不能在有状态组件根元素上使用 v-for,因为它呈现多个元素

标签 javascript html vue.js

获取错误标题中的错误,我是 vue.js 的新手,我无法弄清楚

我一辈子都无法调试这个,有人可以帮忙吗?

const app = new Vue({
    el: '#location-box',
    data: {
        locations: [
            {
                name: "Europe",
                desc: "Loren ipsum"
            },
            {
                name: "America",
                desc: "Loren ipsum"
            }
        ],
    },
})

我的 HTML:

<div id="location-section">
    <div class="main-container">
        <div class="location-grid-container">
            <div id="info-box" class="outline">

            </div>
            <div>
            <div id="location-box" v-for="location in locations" class="outline">
            <h1>{{location.name}}</h1>
            </div>
        </div>
    </div>
</div>

最佳答案

传递给 new Vue 应用程序的 el 选项的根元素必须是唯一的,并且仅用作占位符。

然后您可以在其子项上使用 Vue 指令。

所以在你的情况下你可以这样做:

<div id="location-section">
  <div class="main-container">
    <div class="location-grid-container">
      <div id="info-box" class="outline">
      </div>
      <div id="location-box"><!-- Root element for your Vue app -->
        <div v-for="location in locations" class="outline">
          <h1>{{location.name}}</h1>
        </div>
      </div>
    </div>
  </div>
new Vue({
  el: '#location-box',
  // etc.
});

关于javascript - - 不能在有状态组件根元素上使用 v-for,因为它呈现多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50637898/

相关文章:

javascript - 测试元素是否可以包含文本

javascript - CSS 导航链接悬停

javascript - 用于制作自定义选项卡的最简单的 javascript 库?

html - 使框的大小包装内容

html - 在不禁用内部定义的剪辑路径的情况下隐藏 HTML 中的 SVG 元素?

django - 在数据库中创建帐户条目之前如何处理图像上传?

javascript - here-api 位置在 map 上居中的 svg 图标

HTML:为什么不使用此 css 文件

vue.js - 如何在渲染函数中使用 domProps?

javascript - 使用 setTimeout 了解 Vue 中的 while 循环