当前位置:首页 > 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)实现列表拖拽排序功能。

vue实现list

<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 3 实现的简单登录功能示例,包含表单验证和基础交互逻辑: 创建 Vue 组件 <template> <div class="login-containe…

vue实现用户信息表

vue实现用户信息表

Vue 实现用户信息表的方法 使用 Vue CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tabl…

vue实现文章锚点定位

vue实现文章锚点定位

实现文章锚点定位的方法 在Vue中实现文章锚点定位可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML锚点 通过HTML的id属性和<a>标签的href属性实现锚点跳转。在Vu…

vue 方法实现

vue 方法实现

在 Vue 中,方法的实现通常通过 methods 选项完成。以下是 Vue 方法实现的核心要点和示例: 基本方法定义 在 Vue 组件中定义方法时,需将函数声明放在 methods 对象内。这些方…

vue实现管道

vue实现管道

Vue 中实现管道(Pipe)功能 在 Vue 中可以通过过滤器(Filters)或计算属性(Computed Properties)实现类似管道的功能,将数据经过多个处理步骤后输出。 使用过滤器(…

vue实现oauth

vue实现oauth

Vue 实现 OAuth 的步骤 安装必要的依赖 确保项目中安装了 axios 和 vue-router。OAuth 通常需要发送 HTTP 请求和处理路由跳转。 npm install axios…