当前位置:首页 > VUE

vue简单的列表实现

2026-01-21 22:38:04VUE

实现简单的 Vue 列表

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

<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>

动态添加列表项

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

<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>

使用组件实现可复用列表

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

vue简单的列表实现

<!-- 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 是一个渐进式 JavaScript 框架,适合快速构建用户界面。以下是实现一个简单 Vue 应用的步骤: 安装 Vue 通过 CDN 引入 Vue 是最简单的方式: &…

vue实现列表分类

vue实现列表分类

Vue 实现列表分类的方法 使用计算属性分类 通过计算属性对原始数组进行分类处理,返回分组后的对象或数组。适用于静态数据或需要响应式更新的场景。 computed: { categorizedI…

vue 实现简单登录

vue 实现简单登录

实现 Vue 简单登录功能 创建登录组件 在 Vue 项目中创建一个登录组件 Login.vue,包含用户名和密码输入框以及登录按钮。 <template> <div clas…

vue实现列表点击

vue实现列表点击

Vue 实现列表点击的方法 在 Vue 中实现列表点击功能通常涉及 v-for 渲染列表和 v-on(或 @)绑定点击事件。以下是几种常见实现方式: 基础列表点击 使用 v-for 渲染列表,并通过…

vue实现简单登录

vue实现简单登录

使用 Vue 实现简单登录功能 创建 Vue 项目 确保已安装 Vue CLI,通过以下命令创建新项目: vue create vue-login-demo 选择默认配置或手动配置(如 Babel、…

vue实现树形列表

vue实现树形列表

Vue 实现树形列表的方法 递归组件实现 使用递归组件是最常见的方式,适合处理嵌套层级不确定的数据结构。 数据结构示例 data() { return { treeData: […