当前位置:首页 > VUE

vue实现列表显示

2026-03-27 01:36:09VUE

Vue 列表显示实现方法

在Vue中实现列表显示通常使用v-for指令,结合数组或对象数据源进行渲染。以下是几种常见实现方式:

基础数组列表渲染

通过v-for遍历数组,item代表当前项,index代表可选索引值:

vue实现列表显示

<template>
  <ul>
    <li v-for="(item, index) in items" :key="index">
      {{ item.name }} - {{ item.price }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { name: '商品A', price: 100 },
        { name: '商品B', price: 200 }
      ]
    }
  }
}
</script>

对象属性遍历

v-for也可遍历对象属性,参数顺序为(value, key, index):

<template>
  <div v-for="(value, key) in object" :key="key">
    {{ key }}: {{ value }}
  </div>
</template>

<script>
export default {
  data() {
    return {
      object: {
        title: '标题',
        content: '内容'
      }
    }
  }
}
</script>

使用计算属性过滤列表

结合计算属性实现筛选功能:

vue实现列表显示

<template>
  <input v-model="searchText" placeholder="搜索...">
  <ul>
    <li v-for="item in filteredList" :key="item.id">
      {{ item.text }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      searchText: '',
      fullList: [
        { id: 1, text: '苹果' },
        { id: 2, text: '香蕉' }
      ]
    }
  },
  computed: {
    filteredList() {
      return this.fullList.filter(item => 
        item.text.includes(this.searchText)
      )
    }
  }
}
</script>

动态样式绑定

为列表项添加条件样式:

<template>
  <ul>
    <li 
      v-for="item in items"
      :key="item.id"
      :class="{ 'active': item.isActive }"
      @click="toggleActive(item)"
    >
      {{ item.name }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: '选项1', isActive: false },
        { id: 2, name: '选项2', isActive: true }
      ]
    }
  },
  methods: {
    toggleActive(item) {
      item.isActive = !item.isActive
    }
  }
}
</script>

<style>
.active {
  background-color: #f0f0f0;
}
</style>

使用组件渲染列表

将列表项封装为可复用组件:

<template>
  <ul>
    <ListItem 
      v-for="item in items"
      :key="item.id"
      :item="item"
      @delete="handleDelete"
    />
  </ul>
</template>

<script>
import ListItem from './ListItem.vue'

export default {
  components: { ListItem },
  data() {
    return {
      items: [
        { id: 1, text: '任务1' },
        { id: 2, text: '任务2' }
      ]
    }
  },
  methods: {
    handleDelete(id) {
      this.items = this.items.filter(item => item.id !== id)
    }
  }
}
</script>

性能优化注意事项

  1. 始终为v-for提供唯一的:key值,避免使用索引作为key
  2. 大数据量列表考虑使用虚拟滚动技术
  3. 复杂计算使用计算属性缓存结果
  4. 必要时使用v-once指令优化静态内容

标签: 列表vue
分享给朋友:

相关文章

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 ECh…

vue优秀实现

vue优秀实现

Vue.js 优秀实现方法 响应式数据管理 使用 Vue 的 ref 和 reactive 处理基础类型和对象类型数据,结合 computed 计算属性优化渲染性能。对于复杂状态,推荐 Pinia 替…

vue实现后退

vue实现后退

Vue 实现后退功能的方法 在 Vue 中实现后退功能通常可以通过以下几种方式完成,具体取决于应用场景和需求。 使用 window.history API 通过原生 JavaScript 的 win…

vue架构实现

vue架构实现

Vue 架构实现核心方案 Vue.js 的架构实现通常围绕模块化、组件化和状态管理展开。以下为常见实现方案: 基于单文件组件(SFC)的模块化 通过 .vue 文件组织模板、脚本和样式: <…

vue实现公式

vue实现公式

在Vue中实现公式展示或计算可以通过多种方式实现,以下为具体方法: 使用模板插值与计算属性 若公式较简单,可直接在模板中使用插值表达式或计算属性。例如计算圆的面积: <template>…

vue实现$.extend

vue实现$.extend

Vue 实现类似 jQuery 的 $.extend 功能 在 Vue 中实现类似 jQuery 的 $.extend 功能,可以通过多种方式完成。$.extend 主要用于合并多个对象的属性,Vue…