当前位置:首页 > VUE

vue实现添加内容代码

2026-01-22 11:41:51VUE

以下是一个基于 Vue.js 实现添加内容的代码示例,包含核心逻辑和关键步骤:

基础实现

模板部分(Template)

vue实现添加内容代码

<template>
  <div>
    <input v-model="newItem" placeholder="输入内容" @keyup.enter="addItem">
    <button @click="addItem">添加</button>
    <ul>
      <li v-for="(item, index) in items" :key="index">
        {{ item }}
        <button @click="removeItem(index)">删除</button>
      </li>
    </ul>
  </div>
</template>

脚本部分(Script)

vue实现添加内容代码

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

进阶优化

添加数据验证

methods: {
  addItem() {
    if (!this.newItem.trim()) {
      alert('内容不能为空')
      return
    }
    this.items.push(this.newItem.trim())
    this.newItem = ''
  }
}

使用计算属性

computed: {
  itemCount() {
    return this.items.length
  }
}

完整组件示例

<template>
  <div class="item-manager">
    <div class="input-group">
      <input 
        v-model="newItem" 
        placeholder="输入新内容"
        @keyup.enter="handleAdd"
        class="input-field"
      >
      <button @click="handleAdd" class="add-btn">添加</button>
    </div>

    <div v-if="items.length > 0" class="item-list">
      <div v-for="(item, index) in items" :key="index" class="item">
        <span>{{ item }}</span>
        <button @click="handleRemove(index)" class="remove-btn">×</button>
      </div>
    </div>

    <p v-else class="empty-tip">暂无内容,请添加</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newItem: '',
      items: []
    }
  },
  methods: {
    handleAdd() {
      const content = this.newItem.trim()
      if (!content) return

      this.items.push(content)
      this.newItem = ''
    },
    handleRemove(index) {
      this.items.splice(index, 1)
    }
  }
}
</script>

<style scoped>
.item-manager {
  max-width: 500px;
  margin: 0 auto;
}
.input-group {
  display: flex;
  margin-bottom: 20px;
}
.input-field {
  flex: 1;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}
.add-btn {
  margin-left: 10px;
  padding: 8px 16px;
  background: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
.item-list {
  border: 1px solid #eee;
  border-radius: 4px;
}
.item {
  display: flex;
  justify-content: space-between;
  padding: 10px;
  border-bottom: 1px solid #eee;
}
.item:last-child {
  border-bottom: none;
}
.remove-btn {
  background: none;
  border: none;
  color: #ff6b6b;
  cursor: pointer;
  font-size: 16px;
}
.empty-tip {
  color: #999;
  text-align: center;
  padding: 20px;
}
</style>

关键点说明

  • 使用v-model实现双向数据绑定
  • 通过@keyup.enter监听回车事件
  • 使用数组的push方法添加新项
  • 使用splice方法删除指定项
  • 添加了基本的样式美化
  • 包含空状态提示

可以根据实际需求进一步扩展功能,如添加本地存储、服务端交互等。

标签: 代码内容
分享给朋友:

相关文章

如何生成react代码

如何生成react代码

使用 Create React App 生成项目 安装 Node.js 后,通过命令行工具运行以下命令创建新项目: npx create-react-app my-app cd my-app npm…

vue实现内容轮播

vue实现内容轮播

使用 Vue 实现内容轮播 基础轮播实现 安装依赖(如使用第三方库) 若选择 vue-awesome-swiper,通过以下命令安装: npm install swiper vue-awesom…

vue实现内容拖动

vue实现内容拖动

Vue 实现内容拖动的方法 使用原生 HTML5 拖放 API 在 Vue 中可以利用 HTML5 的拖放 API 实现基本的拖动功能。通过 draggable 属性、dragstart、dragen…

vue抽奖代码实现

vue抽奖代码实现

Vue 抽奖代码实现 基础抽奖组件结构 使用 Vue 3 的 Composition API 实现抽奖功能,核心包括转盘动画、奖品列表和抽奖逻辑。以下为组件基础结构: <template>…

vue分页代码实现

vue分页代码实现

Vue 分页代码实现 在 Vue 中实现分页功能通常需要结合后端 API 返回的数据和前端的分页组件。以下是一个完整的实现示例,包括分页组件的封装和与后端数据的交互。 分页组件封装 创建一个…

vue代码实现管理

vue代码实现管理

Vue.js 管理后台实现方案 使用 Vue.js 构建管理后台系统通常涉及路由、状态管理、UI 组件库等核心模块。以下是关键实现方案: 基础框架搭建 安装 Vue CLI 创建项目: npm i…