…">
当前位置:首页 > 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实现考试多选功能

实现考试多选功能的方法 数据绑定与选项渲染 使用v-model绑定用户选择的答案数组,配合v-for渲染选项列表: <template> <div v-for="(op…

简单实现vue

简单实现vue

Vue 的基本实现 创建一个简单的 Vue 应用需要引入 Vue 库,并编写基本的模板和数据绑定逻辑。 <!DOCTYPE html> <html lang="en"> &l…

原生实现vue功能

原生实现vue功能

原生JavaScript实现Vue核心功能 使用原生JavaScript模拟Vue的核心功能需要理解响应式数据绑定、虚拟DOM和组件系统等概念。以下是关键实现方法: 响应式数据系统 通过Objec…

vue实现功能插件

vue实现功能插件

Vue 插件实现方法 Vue 插件通常用于封装全局功能(如指令、过滤器、混入等),以下是实现 Vue 插件的典型方式: 插件基本结构 const MyPlugin = { install(…

vue修改功能实现

vue修改功能实现

Vue 修改功能的实现方法 在 Vue 中实现修改功能通常涉及表单绑定、数据更新和事件处理。以下是几种常见的实现方式: 数据双向绑定 使用 v-model 指令实现表单元素与 Vue 实例数据的双…

vue实现简单表格

vue实现简单表格

Vue 实现简单表格的方法 使用原生 HTML 表格 在 Vue 模板中可以直接使用 HTML 原生表格标签,通过 v-for 动态渲染数据: <template> <tabl…