当前位置:首页 > VUE

vue简单的列表实现

2026-01-21 22:38:04VUE

实现简单的 Vue 列表

在 Vue 中实现列表功能通常使用 v-for 指令。以下是一个基础的列表实现示例:

vue简单的列表实现

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

<script>
export default {
  data() {
    return {
      items: [
        { name: '苹果', price: 5 },
        { name: '香蕉', price: 3 },
        { name: '橙子', price: 4 }
      ]
    }
  }
}
</script>

动态添加列表项

可以通过方法动态向列表中添加新项:

vue简单的列表实现

<template>
  <div>
    <input v-model="newItem.name" placeholder="名称">
    <input v-model="newItem.price" placeholder="价格" type="number">
    <button @click="addItem">添加</button>

    <ul>
      <li v-for="(item, index) in items" :key="index">
        {{ item.name }} - {{ item.price }}
        <button @click="removeItem(index)">删除</button>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newItem: { name: '', price: 0 },
      items: []
    }
  },
  methods: {
    addItem() {
      this.items.push({...this.newItem})
      this.newItem = { name: '', price: 0 }
    },
    removeItem(index) {
      this.items.splice(index, 1)
    }
  }
}
</script>

使用计算属性过滤列表

可以使用计算属性实现列表过滤功能:

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索...">

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

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [
        '苹果', '香蕉', '橙子', '西瓜', '葡萄'
      ]
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => 
        item.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

使用组件实现可复用列表

可以将列表封装为可复用组件:

<!-- ItemList.vue -->
<template>
  <ul>
    <slot v-for="(item, index) in items" :item="item" :index="index"></slot>
  </ul>
</template>

<script>
export default {
  props: ['items']
}
</script>

<!-- 使用示例 -->
<template>
  <ItemList :items="fruits">
    <template v-slot="{ item, index }">
      <li>{{ index + 1 }}. {{ item }}</li>
    </template>
  </ItemList>
</template>

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

export default {
  components: { ItemList },
  data() {
    return {
      fruits: ['苹果', '香蕉', '橙子']
    }
  }
}
</script>

这些示例展示了 Vue 中实现列表功能的基本方法,可以根据实际需求进行扩展和调整。

标签: 简单列表
分享给朋友:

相关文章

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。ite…

vue 简单实现

vue 简单实现

以下是 Vue 的简单实现示例,涵盖基础模板、数据绑定、方法及生命周期: 基础模板与数据绑定 <div id="app"> <p>{{ message }}</p&…

vue实现商品列表

vue实现商品列表

Vue实现商品列表的方法 使用Vue实现商品列表需要结合数据绑定、组件化和状态管理。以下是实现商品列表的几种常见方法: 基础数据绑定实现 在Vue组件中定义商品数据数组,使用v-for指令循环渲染…

vue实现列表水印

vue实现列表水印

实现列表水印的方法 在Vue中为列表添加水印可以通过多种方式实现,以下是几种常见的方法: 使用CSS背景图 通过CSS的background-image属性为列表元素添加水印背景。水印可以是文字或…

vue实现列表显示

vue实现列表显示

使用 Vue 实现列表显示 基础列表渲染 在 Vue 中,可以通过 v-for 指令实现列表渲染。以下是一个简单的示例: <template> <ul> <…

vue公共列表的实现

vue公共列表的实现

Vue 公共列表的实现方法 在 Vue 中实现公共列表组件,可以通过封装可复用的逻辑和模板来完成。以下是几种常见的实现方式: 使用 props 和 slots 通过 props 接收列表数据,使用…