当前位置:首页 > 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中实现组件缓存可以通过内置的<keep-alive>组件完成,该组件能够缓存不活动的组件实例,避免重复渲染。 使用<keep-alive>基…

uniapp组件

uniapp组件

uniapp组件基础概念 uniapp的组件系统基于Vue.js,支持跨平台开发(H5、小程序、App等)。组件分为内置基础组件(如<view>、<button>)和自定义组件…

vue实现折叠组件

vue实现折叠组件

实现折叠组件的基本思路 在Vue中实现折叠组件通常需要利用动态绑定和条件渲染。核心是通过控制一个布尔值状态来决定内容是否显示,并添加过渡动画提升用户体验。 基础实现方法 使用v-show或v-if控…

vue抽屉组件实现

vue抽屉组件实现

Vue 抽屉组件实现 使用 Element UI 实现 Element UI 提供了现成的抽屉组件 el-drawer,可以快速实现抽屉效果。 安装 Element UI: npm install…

vue实现日历组件

vue实现日历组件

Vue 日历组件实现步骤 基础结构搭建 使用Vue 3的Composition API或Options API创建组件框架。核心结构包括年份/月份切换按钮、星期标题栏和日期网格。 <t…

vue 实现弹窗组件

vue 实现弹窗组件

Vue 弹窗组件实现 基础弹窗组件结构 创建一个名为Modal.vue的文件,定义弹窗的基础结构和样式: <template> <div class="modal-mask"…