当前位置:首页 > 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实现好评弹框

Vue 实现好评弹框的方法 使用 Vue 实现好评弹框可以通过组件化方式完成,结合动态数据绑定和事件处理。以下是具体实现步骤: 1. 创建弹框组件 新建一个 RatingDialog.vue 组件…

vue实现付款

vue实现付款

Vue 实现付款功能 在 Vue 中实现付款功能通常需要集成第三方支付网关(如支付宝、微信支付、Stripe 等)。以下是常见的实现方法: 集成支付宝/微信支付 安装必要的依赖(如 axios 用于…

vue实现报表

vue实现报表

vue实现报表的方法 使用Vue实现报表通常需要结合图表库或表格组件。以下是几种常见方法: 使用ECharts 安装ECharts库: npm install echarts vue-echart…

vue 实现关闭

vue 实现关闭

Vue 实现关闭功能的方法 在Vue中实现关闭功能通常涉及组件销毁、模态框隐藏或路由跳转等场景。以下是几种常见实现方式: 销毁当前组件 使用v-if或v-show控制组件显示/隐藏,适用于局部组件:…

vue实现vnc

vue实现vnc

Vue 实现 VNC 客户端 在 Vue 项目中实现 VNC 客户端功能,可以通过集成现有的 VNC 客户端库或组件完成。以下是具体实现方法: 安装依赖库 使用 noVNC 或其他 VNC 客户端…

javascript实现vue

javascript实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,主要包括数据绑定、响应式系统和虚拟 DOM。以下是一个简化的实现示例。 数据响应式系统 通过 Object.de…