当前位置:首页 > 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>

动态绑定列表项属性

可以为列表项动态绑定属性或样式,实现交互效果。

vue实现list

<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 组件封装列表项

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

vue实现list

<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)优化性能。

标签: vuelist
分享给朋友:

相关文章

vue前端实现下载进度

vue前端实现下载进度

实现下载进度条的基本思路 在Vue中实现下载进度条,通常需要结合XMLHttpRequest或Fetch API来监听下载进度事件。通过计算已下载数据与总数据的比例,动态更新进度条的显示。 使用XM…

vue广告实现

vue广告实现

Vue 广告实现方法 在 Vue 项目中实现广告功能可以通过多种方式完成,具体取决于广告类型和需求。以下是几种常见的实现方法: 使用第三方广告平台 许多广告平台如 Google AdSense、百…

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template> &…

vue实现滚屏

vue实现滚屏

实现滚屏的基本方法 在Vue中实现滚屏效果可以通过多种方式完成,常见的有原生JavaScript滚动方法、第三方库或CSS动画。以下是几种常见实现方式: 使用window.scrollTo或Elem…

vue实现eventbus

vue实现eventbus

Vue 中实现 EventBus 在 Vue 中,EventBus 是一种跨组件通信的机制,尤其适用于非父子组件之间的数据传递。以下是实现 EventBus 的几种方法: 方法一:使用 Vue 实例…

vue实现hexo

vue实现hexo

Vue 集成 Hexo 的实现方法 Hexo 是一个静态博客框架,而 Vue 是一个前端框架。将 Vue 集成到 Hexo 中可以通过以下方式实现: 在 Hexo 中使用 Vue 组件 通过 Hex…