…">
当前位置:首页 > VUE

vue简单功能实现

2026-01-17 07:36:16VUE

Vue 简单功能实现示例

以下是一个基于 Vue 3 的简单计数器功能实现,包含模板、脚本和样式部分:

<template>
  <div class="counter">
    <h3>当前计数:{{ count }}</h3>
    <button @click="increment">增加</button>
    <button @click="decrement">减少</button>
    <button @click="reset">重置</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      count: 0
    }
  },
  methods: {
    increment() {
      this.count++
    },
    decrement() {
      this.count--
    },
    reset() {
      this.count = 0
    }
  }
}
</script>

<style scoped>
.counter {
  text-align: center;
  margin: 20px;
}
button {
  margin: 0 5px;
  padding: 8px 16px;
  cursor: pointer;
}
</style>

待办事项列表实现

这是一个简单的待办事项列表功能:

<template>
  <div>
    <input v-model="newTodo" @keyup.enter="addTodo" placeholder="添加新任务">
    <ul>
      <li v-for="(todo, index) in todos" :key="index">
        {{ todo.text }}
        <button @click="removeTodo(index)">删除</button>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newTodo: '',
      todos: []
    }
  },
  methods: {
    addTodo() {
      if (this.newTodo.trim()) {
        this.todos.push({ text: this.newTodo })
        this.newTodo = ''
      }
    },
    removeTodo(index) {
      this.todos.splice(index, 1)
    }
  }
}
</script>

表单双向绑定示例

实现一个简单的用户注册表单:

<template>
  <form @submit.prevent="submitForm">
    <div>
      <label>用户名:</label>
      <input v-model="form.username" type="text">
    </div>
    <div>
      <label>密码:</label>
      <input v-model="form.password" type="password">
    </div>
    <div>
      <label>邮箱:</label>
      <input v-model="form.email" type="email">
    </div>
    <button type="submit">注册</button>
  </form>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: '',
        email: ''
      }
    }
  },
  methods: {
    submitForm() {
      console.log('提交的表单数据:', this.form)
      // 这里可以添加表单验证和提交逻辑
    }
  }
}
</script>

条件渲染示例

根据条件显示不同内容的实现:

<template>
  <div>
    <button @click="toggleShow">切换显示</button>
    <p v-if="show">现在你看到我了</p>
    <p v-else>现在你看不到我了</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      show: true
    }
  },
  methods: {
    toggleShow() {
      this.show = !this.show
    }
  }
}
</script>

计算属性示例

使用计算属性实现一个简单的购物车总价计算:

<template>
  <div>
    <div v-for="(item, index) in cart" :key="index">
      {{ item.name }} - ¥{{ item.price }} × {{ item.quantity }}
    </div>
    <h3>总价:¥{{ totalPrice }}</h3>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cart: [
        { name: '商品A', price: 100, quantity: 2 },
        { name: '商品B', price: 200, quantity: 1 },
        { name: '商品C', price: 50, quantity: 3 }
      ]
    }
  },
  computed: {
    totalPrice() {
      return this.cart.reduce((total, item) => {
        return total + (item.price * item.quantity)
      }, 0)
    }
  }
}
</script>

这些示例涵盖了 Vue 的核心功能,包括数据绑定、事件处理、条件渲染、列表渲染和计算属性等基本概念。

vue简单功能实现

标签: 简单功能
分享给朋友:

相关文章

vue简单实现

vue简单实现

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

vue实现tab功能

vue实现tab功能

Vue 实现 Tab 功能的方法 使用动态组件和 v-if 指令 通过 v-if 或 v-show 控制不同 Tab 内容的显示与隐藏。这种方法适合简单的 Tab 切换需求。 <tem…

vue实现拨号功能

vue实现拨号功能

Vue实现拨号功能的方法 使用HTML5的tel协议 在Vue模板中直接使用<a>标签的tel:协议实现拨号功能。这种方法适用于移动端浏览器,点击后会调起系统的拨号界面。 <tem…

vue 实现收藏功能

vue 实现收藏功能

实现收藏功能的基本思路 在Vue中实现收藏功能通常涉及前端交互与后端数据存储的结合。核心逻辑包括:用户点击收藏按钮时切换状态,并通过API将状态同步到后端数据库。 前端组件实现 创建收藏按钮组件,…

vue实现聊天功能

vue实现聊天功能

Vue实现聊天功能的方法 使用WebSocket实现实时通信 安装WebSocket客户端库如socket.io-client,在Vue组件中建立连接。创建一个消息列表和数据输入框,通过WebSock…

vue实现桌面功能

vue实现桌面功能

Vue 实现桌面功能的方法 Vue.js 可以通过结合 Electron 或 NW.js 等框架实现桌面应用开发。以下是几种常见的方法: 使用 Vue 与 Electron 结合 Electron…