当前位置:首页 > VUE

vue实现列表组件

2026-02-17 21:13:44VUE

Vue 列表组件实现方法

基础列表渲染

使用 v-for 指令可以轻松实现列表渲染。通过遍历数组或对象,动态生成列表项。

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

<script>
export default {
  data() {
    return {
      items: [
        { name: 'Item 1' },
        { name: 'Item 2' },
        { name: 'Item 3' }
      ]
    }
  }
}
</script>

动态绑定属性

列表项可以绑定动态属性和事件,实现交互功能。

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

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1', isActive: false },
        { id: 2, name: 'Item 2', isActive: false },
        { id: 3, name: 'Item 3', isActive: false }
      ]
    }
  },
  methods: {
    toggleActive(index) {
      this.items[index].isActive = !this.items[index].isActive
    }
  }
}
</script>

列表过滤与排序

通过计算属性可以实现列表的过滤和排序功能。

<template>
  <div>
    <input v-model="searchQuery" placeholder="Search...">
    <ul>
      <li v-for="item in filteredItems" :key="item.id">
        {{ item.name }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: 'Apple' },
        { id: 2, name: 'Banana' },
        { id: 3, name: 'Orange' }
      ]
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => 
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

可编辑列表

实现一个可以编辑的列表组件,允许用户修改列表项内容。

<template>
  <ul>
    <li v-for="(item, index) in items" :key="item.id">
      <input v-if="item.isEditing" v-model="item.name" @blur="item.isEditing = false">
      <span v-else @click="item.isEditing = true">{{ item.name }}</span>
      <button @click="removeItem(index)">Remove</button>
    </li>
    <button @click="addItem">Add Item</button>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1', isEditing: false },
        { id: 2, name: 'Item 2', isEditing: false }
      ]
    }
  },
  methods: {
    addItem() {
      this.items.push({
        id: Date.now(),
        name: 'New Item',
        isEditing: true
      })
    },
    removeItem(index) {
      this.items.splice(index, 1)
    }
  }
}
</script>

性能优化

对于大型列表,可以使用 v-forkey 属性和 Vue 的虚拟滚动技术优化性能。

<template>
  <div style="height: 300px; overflow-y: auto">
    <div v-for="item in largeList" :key="item.id" style="height: 50px">
      {{ item.name }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      largeList: Array.from({ length: 1000 }, (_, i) => ({
        id: i,
        name: `Item ${i + 1}`
      }))
    }
  }
}
</script>

组件化列表

将列表项封装为单独组件,提高代码复用性和可维护性。

vue实现列表组件

<template>
  <ul>
    <list-item 
      v-for="item in items" 
      :key="item.id"
      :item="item"
      @remove="removeItem"
    />
  </ul>
</template>

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

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

以上方法涵盖了 Vue 列表组件的主要实现方式,可以根据实际需求选择适合的方案或组合使用。

标签: 组件列表
分享给朋友:

相关文章

vue实现拖拽实现组件嵌套

vue实现拖拽实现组件嵌套

Vue 拖拽实现组件嵌套 使用 Vue 实现拖拽组件嵌套功能可以通过原生 HTML5 拖拽 API 或第三方库如 vuedraggable 实现。以下是两种方法的详细说明: 使用 HTML5 拖拽…

vue 实现长列表

vue 实现长列表

vue 实现长列表的优化方法 使用虚拟滚动技术,只渲染可视区域内的元素,大幅减少DOM节点数量。通过计算滚动位置动态更新显示内容,降低内存占用和渲染压力。 <template> &l…

vue实现组件循环图片

vue实现组件循环图片

Vue 实现组件循环图片的方法 在 Vue 中实现组件循环图片通常可以通过 v-for 指令结合动态数据绑定完成。以下是几种常见的实现方式: 使用静态图片路径数组 假设有一组本地图片路径,可以通过数…

vue实现列表添加

vue实现列表添加

使用 Vue 实现列表添加功能 数据绑定与列表渲染 在 Vue 中,通过 v-for 指令可以轻松实现列表渲染。首先需要定义一个数组来存储列表数据,并在模板中使用 v-for 循环渲染。 <t…

uniapp 分页组件

uniapp 分页组件

uniapp 分页组件实现方法 在uniapp中实现分页功能,可以通过自定义组件或使用第三方组件库完成。以下是几种常见实现方式: 自定义分页组件 创建一个名为uni-pagination的组件,模板…

vue实现列表页

vue实现列表页

Vue 实现列表页的方法 数据绑定与渲染 使用 v-for 指令动态渲染列表数据,结合 :key 确保渲染性能。示例代码: <template> <ul> <…