当前位置:首页 > VUE

vue实现添加内容代码

2026-01-22 11:41:51VUE

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

基础实现

模板部分(Template)

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

<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方法删除指定项
  • 添加了基本的样式美化
  • 包含空状态提示

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

vue实现添加内容代码

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

相关文章

淘宝css代码制作

淘宝css代码制作

在淘宝店铺装修中,CSS代码用于自定义页面样式,提升视觉效果和用户体验。以下是常见的CSS代码制作方法和应用场景: 自定义店铺背景 通过CSS可以修改店铺背景颜色或图片,代码示例如下: body…

vue扫码登录代码实现

vue扫码登录代码实现

Vue 扫码登录实现方案 准备工作 确保项目已安装 Vue 2/3 及相关依赖,需要后端提供生成二维码和验证扫码状态的接口。 生成二维码 使用 qrcode 库生成登录二维码,需调用后端接口获取唯一…

jquery代码

jquery代码

以下是关于jQuery代码的常见用法和示例,涵盖基础操作、事件处理、DOM操作等核心功能: 基础语法 jQuery通过$()函数选择元素,支持CSS选择器语法: // 选择所有<p>…

react如何代码优化

react如何代码优化

减少不必要的重新渲染 使用 React.memo 包装函数组件以避免在 props 未变化时重新渲染。对于类组件,可以通过 shouldComponentUpdate 或继承 PureComponen…

php实现文件下载代码

php实现文件下载代码

实现文件下载的基本方法 使用PHP实现文件下载的核心是通过设置HTTP头部信息,强制浏览器触发下载行为而非直接显示文件内容。 $file_path = 'path/to/your/file.ext…

js实现代码雨

js实现代码雨

实现代码雨效果 使用HTML5 Canvas和JavaScript可以轻松实现经典的代码雨效果。以下是完整的实现代码和说明: HTML结构 <!DOCTYPE html> <ht…