当前位置:首页 > VUE

vue实现list

2026-01-13 00:29:37VUE

Vue 实现列表(List)的常用方法

使用 v-for 指令渲染列表

在 Vue 中,v-for 是渲染列表的核心指令。通过遍历数组或对象,动态生成 DOM 元素。

<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="index"
      :class="{ 'active': item.isActive }"
      @click="toggleActive(index)"
    >
      {{ item.name }}
    </li>
  </ul>
</template>

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

使用计算属性过滤列表

通过计算属性对列表数据进行过滤或排序,实现动态更新。

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

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

使用 Vue 组件封装列表项

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

<template>
  <ul>
    <ListItem 
      v-for="(item, index) in items" 
      :key="index"
      :item="item"
      @delete="handleDelete(index)"
    />
  </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' },
        { id: 3, name: 'Item 3' }
      ]
    };
  },
  methods: {
    handleDelete(index) {
      this.items.splice(index, 1);
    }
  }
};
</script>

列表分页实现

通过分页逻辑控制列表的显示范围,优化性能。

<template>
  <ul>
    <li v-for="(item, index) in paginatedItems" :key="index">
      {{ item.name }}
    </li>
  </ul>
  <button @click="prevPage" :disabled="currentPage === 1">Previous</button>
  <button @click="nextPage" :disabled="currentPage === totalPages">Next</button>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      itemsPerPage: 5,
      items: [
        { name: 'Item 1' },
        { name: 'Item 2' },
        // ...更多数据
      ]
    };
  },
  computed: {
    totalPages() {
      return Math.ceil(this.items.length / this.itemsPerPage);
    },
    paginatedItems() {
      const start = (this.currentPage - 1) * this.itemsPerPage;
      const end = start + this.itemsPerPage;
      return this.items.slice(start, end);
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++;
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--;
      }
    }
  }
};
</script>

拖拽排序列表

通过第三方库(如 vuedraggable)实现列表拖拽排序功能。

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

<script>
import draggable from 'vuedraggable';

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

注意事项

  • 始终为 v-for 提供唯一的 :key,通常使用 id 而非 index
  • 避免直接修改 props 中的列表数据,使用事件或 Vuex 管理状态。
  • 大数据量列表建议使用虚拟滚动(如 vue-virtual-scroller)优化性能。

vue实现list

标签: vuelist
分享给朋友:

相关文章

vue实现右下角弹框

vue实现右下角弹框

实现右下角弹框的基本思路 在Vue中实现右下角弹框,通常需要结合CSS定位和Vue的组件化特性。弹框可以通过绝对定位固定在右下角,并通过Vue控制其显示与隐藏。 创建弹框组件 新建一个Vue组件(如…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

vue实现画图

vue实现画图

Vue 实现画图功能 在 Vue 中实现画图功能通常需要结合 HTML5 的 <canvas> 元素或第三方库。以下是几种常见的方法: 使用原生 Canvas API 通过 Vue 直…

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <tem…

vue代理实现

vue代理实现

Vue 代理实现方法 在 Vue 项目中,配置代理可以解决开发环境下的跨域问题,通常通过修改 vue.config.js 文件实现。以下是几种常见的代理配置方式: 基础代理配置 在 vue.con…

vue实现treeview

vue实现treeview

Vue 实现 TreeView 的方法 使用递归组件实现 TreeView 递归组件是 Vue 中实现 TreeView 的常见方式。通过组件调用自身,可以轻松处理嵌套的树形结构数据。 <te…