当前位置:首页 > VUE

vue实现列表添加

2026-01-14 04:04:21VUE

使用 Vue 实现列表添加功能

数据绑定与列表渲染

在 Vue 中,通过 v-for 指令可以轻松实现列表渲染。首先需要定义一个数组来存储列表数据,并在模板中使用 v-for 循环渲染。

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

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

添加新项到列表

通过 v-model 绑定输入框的值,并在方法中使用 push 将新项添加到数组。

vue实现列表添加

<template>
  <div>
    <input v-model="newItem" placeholder="Add new item">
    <button @click="addItem">Add</button>
    <ul>
      <li v-for="(item, index) in items" :key="index">
        {{ item }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['Item 1', 'Item 2', 'Item 3'],
      newItem: ''
    }
  },
  methods: {
    addItem() {
      if (this.newItem.trim() !== '') {
        this.items.push(this.newItem)
        this.newItem = ''
      }
    }
  }
}
</script>

删除列表项

为每个列表项添加删除按钮,并通过 splice 方法移除指定项。

vue实现列表添加

<template>
  <div>
    <input v-model="newItem" placeholder="Add new item">
    <button @click="addItem">Add</button>
    <ul>
      <li v-for="(item, index) in items" :key="index">
        {{ item }}
        <button @click="removeItem(index)">Delete</button>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['Item 1', 'Item 2', 'Item 3'],
      newItem: ''
    }
  },
  methods: {
    addItem() {
      if (this.newItem.trim() !== '') {
        this.items.push(this.newItem)
        this.newItem = ''
      }
    },
    removeItem(index) {
      this.items.splice(index, 1)
    }
  }
}
</script>

使用计算属性优化

如果需要过滤或处理列表数据,可以使用计算属性。

<template>
  <div>
    <input v-model="newItem" placeholder="Add new item">
    <button @click="addItem">Add</button>
    <ul>
      <li v-for="(item, index) in filteredItems" :key="index">
        {{ item }}
        <button @click="removeItem(index)">Delete</button>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['Item 1', 'Item 2', 'Item 3'],
      newItem: ''
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => item.includes('Item'))
    }
  },
  methods: {
    addItem() {
      if (this.newItem.trim() !== '') {
        this.items.push(this.newItem)
        this.newItem = ''
      }
    },
    removeItem(index) {
      this.items.splice(index, 1)
    }
  }
}
</script>

使用 Vue 3 的 Composition API

在 Vue 3 中,可以使用 refreactive 来管理状态。

<template>
  <div>
    <input v-model="newItem" placeholder="Add new item">
    <button @click="addItem">Add</button>
    <ul>
      <li v-for="(item, index) in items" :key="index">
        {{ item }}
        <button @click="removeItem(index)">Delete</button>
      </li>
    </ul>
  </div>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const items = ref(['Item 1', 'Item 2', 'Item 3'])
    const newItem = ref('')

    const addItem = () => {
      if (newItem.value.trim() !== '') {
        items.value.push(newItem.value)
        newItem.value = ''
      }
    }

    const removeItem = (index) => {
      items.value.splice(index, 1)
    }

    return {
      items,
      newItem,
      addItem,
      removeItem
    }
  }
}
</script>

标签: 列表vue
分享给朋友:

相关文章

vue实现微博印象

vue实现微博印象

实现微博印象功能的核心思路 微博印象功能通常指用户发布微博后,系统自动分析文本内容并提取关键词或标签,以可视化形式展示。Vue实现该功能需要结合文本分析、关键词提取和前端渲染技术。 文本分析与关键词…

vue实现走势图

vue实现走势图

Vue 实现走势图的方法 使用 ECharts 库 ECharts 是一个强大的数据可视化库,支持多种图表类型,包括走势图(折线图)。在 Vue 项目中可以通过 vue-echarts 封装库或直接使…

vue实现grid

vue实现grid

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

vue实现pie

vue实现pie

Vue 实现饼图 在 Vue 中实现饼图通常可以通过第三方图表库(如 ECharts、Chart.js 或 Highcharts)来完成。以下是使用 ECharts 和 Chart.js 的两种常见方…

vue实现list

vue实现list

Vue 实现列表渲染 在 Vue 中,可以使用 v-for 指令来渲染列表。v-for 指令基于一个数组或对象来渲染一个列表,可以遍历数组或对象的属性。 基本列表渲染 <template>…

vue简单实现

vue简单实现

Vue 简单实现示例 以下是一个使用 Vue.js 创建简单应用的示例,涵盖基本功能如数据绑定、事件处理和组件化。 基础 HTML 结构 <!DOCTYPE html> <html…