vue for循环使用v-if报错

vue yekong 109℃

vue项目开发中,出现在使用v-if的时候报错了

 TypeError: Cannot read properties of undefined (reading 'show')
<div class="item" v-for="(item,index) in list" :key="index"
           draggable="true"
           @mousedown="dragStart($event, index)"
           v-if="item.show"
           :style="{ left: item.left + 'px', top: item.top + 'px', width: item.width + 'px', height: item.height + 'px' }">
        <item :delay="0" :icon="item.icon" :name="item.name">
        </item>
      </div>   list: [
        {
          left: 50,
          top: 93,
          width: 403,
          height: 270,
          name: '项目进度',
          icon: icon6,
          ref: 'item0',
          show: false,
        }, {
          left: 491,
          top: 799,
          width: 416,
          height: 282,
          name: '各个进度',
          icon: icon7,
          ref: 'item1',
          show: true,
        }, {
          left: 50,
          top: 795,
          width: 395,
          height: 307,
          name: '最新进度',
          icon: icon8,
          ref: 'item2',
          show: true,
        }, {
          left: 49,
          top: 479,
          width: 369,
          height: 299,
          name: '小组进度',
          icon: icon3,
          ref: 'item3',
          show: true,
        }, {
          left: 968,
          top: 795,
          width: 445,
          height: 311,
          name: '参数',
          icon: icon4,
          ref: 'item4',
          show: true,
        }, {
          left: 954,
          top: 429,
          width: 454,
          height: 317,
          name: '住宅信息统计',
          icon: icon9,
          ref: 'item5',
          show: true,
        }, {
          left: 951,
          top: 85,
          width: 448,
          height: 331,
          name: '安置方式',
          icon: icon10,
          ref: 'item6',
          show: true,
        }, {
          left: 1459,
          top: 677,
          width: 478,
          height: 289,
          name: '照顾对象',
          icon: icon11,
          ref: 'item7',
          show: true,
        }, {
          left: 1454,
          top: 395,
          width: 487,
          height: 257,
          name: '非住宅信息统计',
          icon: icon1,
          ref: 'item8',
          show: true,
        }, {
          left: 1452,
          top: 85,
          width: 505,
          height: 309,
          name: '四类非住宅比例',
          icon: icon2,
          ref: 'item9',
          show: true,
        }], 

错误信息 TypeError: Cannot read properties of undefined (reading 'show') 表示你尝试访问一个 undefined 对象的 show 属性。在代码中,这个错误可能发生在 v-for 循环中,当你尝试访问 item.show 时。

这个错误通常发生在以下几种情况:

  1. 列表 list 中的某个元素不存在:这可能是因为 list 数组在渲染之前被修改了,或者在异步数据加载过程中,你尝试访问了一个还未定义的元素。

  2. 索引超出数组范围:如果 index 超出了 list 数组的范围,item 将会是 undefined,尝试访问 item.show 就会抛出错误。

  3. 数据绑定问题:可能是数据没有正确绑定到组件,或者在数据更新后,视图尝试访问了一个已经不存在的元素。

为了解决这个问题,你可以采取以下步骤:

检查 list 数组

确保 list 数组在组件的 data 中定义,并且在任何时候都包含了正确的元素。如果 list 是从异步数据源加载的,确保在数据加载完成之前不要渲染依赖于该数据的组件部分。

使用 v-if 来防止渲染未定义的元素

如果 list 数组可能会在组件的生命周期中发生变化,你可以使用 v-if 来确保只有在 item 确实存在时才渲染相关的 DOM 元素。

<div v-if="list.length > 0">
  <div class="item" v-for="(item, index) in list" :key="index"
       draggable="true"
       @mousedown="dragStart($event, index)"
       v-if="item.show"
       :style="{ left: item.left + 'px', top: item.top + 'px', width: item.width + 'px', height: item.height + 'px' }">
    <!-- ... -->
  </div>
</div>

使用 v-forv-if 的替代方案

在Vue中,不推荐在同一个元素上同时使用 v-forv-ifv-for 拥有比 v-if 更高的优先级,所以如果你需要根据条件过滤列表,可以在计算属性中进行过滤:

computed: {
  filteredList() {
    return this.list.filter(item => item.show);
  }
}

然后在模板中使用这个计算属性:

<div class="item" v-for="(item, index) in filteredList" :key="index"
     draggable="true"
     @mousedown="dragStart($event, index)"
     :style="{ left: item.left + 'px', top: item.top + 'px', width: item.width + 'px', height: item.height + 'px' }">
  <!-- ... -->
</div>

通过以上步骤,你应该能够解决 TypeError: Cannot read properties of undefined (reading 'show') 的问题。确保在渲染之前 list 数组中的每个元素都已经定义,并且在组件的整个生命周期中都保持数据的一致性。

喜欢 (0)