【问题标题】:Vue js "Cannot read property 'raw' of undefined" on v-for nested loopv-for嵌套循环上的Vue js“无法读取未定义的属性'原始'”
【发布时间】:2016-06-07 14:02:20
【问题描述】:

你能帮我解决这个问题吗?我收到此错误

无法读取未定义的属性“原始”

当我点击第二个链接时。

HTML

<div id="demo">
  <div class="item">
    <div class="menu">
      <a href="javascript:void(0)" id="object_1" @click.prevent="onClick">object 1</a> -
      <a href="javascript:void(0)" id="object_2" @click.prevent="onClick">object 2</a>
    </div>
  </div>
  <div class="main">
    <!-- 1st LOOP on main object -->
    <template v-for="object in objects" v-if="object.visible">
      <!--  2nd LOOP on children  -->
      <template v-for="children1 in object">
        <!--  3rd LOOP on array  -->
        <template v-for="children2 in children1">
          {{children2 | json}}
        </template>
      </template>
    </template>
  </div>
</div>

JS

new Vue({
  el: '#demo',
  data: {
    "objects": {
      "object_1": {
        "visible": false,
        "children": [{
          "string": "string 1"
        }, {
          "string": "string 2"
        }]
      },
      "object_2": {
        "visible": false,
        "children": [{
          "string": "string 3"
        }, {
          "string": "string 4"
        }]
      }
    }
  },
  methods: {
    onClick: function(event) {
      var getID = event.path[0].id
      for (object in vm.objects) {
        vm.$set(`objects.${object}.visible`, false)
      }
      vm.$set(`objects.${getID}.visible`, true)
    }
  }
})

http://jsbin.com/gesigohuqo

【问题讨论】:

    标签: javascript vue.js


    【解决方案1】:

    你不需要 3 个循环:

    <!-- 1st LOOP on main object -->
    <template v-for="object in objects" v-if="object.visible">
      <!--  2nd LOOP on array  -->
      <template v-for="children in object.children">
        {{ children | json }}
      </template>
    </template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 2020-10-09
      • 2021-08-28
      • 1970-01-01
      • 2018-05-08
      相关资源
      最近更新 更多