当前位置:首页 > VUE

vue简单功能实现

2026-02-18 00:04:30VUE

Vue 实现计数器功能

创建一个基础的计数器组件,包含增加、减少和重置功能。

<template>
  <div>
    <p>当前计数: {{ count }}</p>
    <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>

Vue 实现待办事项列表

构建一个简单的待办事项管理功能,支持添加和删除任务。

vue简单功能实现

<template>
  <div>
    <input v-model="newTodo" @keyup.enter="addTodo" placeholder="添加新任务">
    <ul>
      <li v-for="(todo, index) in todos" :key="index">
        {{ todo }}
        <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(this.newTodo)
        this.newTodo = ''
      }
    },
    removeTodo(index) {
      this.todos.splice(index, 1)
    }
  }
}
</script>

Vue 实现表单验证

创建一个带有基本验证功能的表单组件。

vue简单功能实现

<template>
  <form @submit.prevent="submitForm">
    <div>
      <label>用户名:</label>
      <input v-model="username" type="text">
      <span v-if="errors.username">{{ errors.username }}</span>
    </div>
    <div>
      <label>密码:</label>
      <input v-model="password" type="password">
      <span v-if="errors.password">{{ errors.password }}</span>
    </div>
    <button type="submit">提交</button>
  </form>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      errors: {}
    }
  },
  methods: {
    validateForm() {
      this.errors = {}
      if (!this.username) this.errors.username = '用户名不能为空'
      if (!this.password) this.errors.password = '密码不能为空'
      return Object.keys(this.errors).length === 0
    },
    submitForm() {
      if (this.validateForm()) {
        console.log('表单提交:', {
          username: this.username,
          password: this.password
        })
      }
    }
  }
}
</script>

Vue 实现条件渲染

使用v-if和v-show指令实现条件渲染功能。

<template>
  <div>
    <button @click="toggleShow">切换显示</button>
    <p v-if="isVisible">使用v-if渲染的内容</p>
    <p v-show="isVisible">使用v-show渲染的内容</p>
  </div>
</template>

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

Vue 实现计算属性

展示计算属性的基本用法,自动计算并缓存结果。

<template>
  <div>
    <input v-model="firstName" placeholder="名">
    <input v-model="lastName" placeholder="姓">
    <p>全名: {{ fullName }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      firstName: '',
      lastName: ''
    }
  },
  computed: {
    fullName() {
      return `${this.firstName} ${this.lastName}`.trim()
    }
  }
}
</script>

这些示例涵盖了Vue的核心功能,包括数据绑定、事件处理、条件渲染、列表渲染、表单处理和计算属性等基本概念。每个示例都可以直接复制到Vue项目中运行,适合初学者理解和实践Vue的基本功能实现。

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

相关文章

vue实现简单的弹窗

vue实现简单的弹窗

Vue实现简单弹窗的方法 使用v-if和v-show控制显示 通过Vue的指令v-if或v-show可以轻松控制弹窗的显示与隐藏。v-if会完全销毁和重建DOM元素,而v-show仅通过CSS的di…

vue实现功能

vue实现功能

Vue 实现功能的方法 Vue.js 是一个流行的前端框架,用于构建用户界面。以下是几种常见的功能实现方法: 数据绑定 Vue 提供了双向数据绑定,可以通过 v-model 指令实现表单输入和应用状…

简单实现vue

简单实现vue

实现一个简易 Vue 框架 以下是一个基于 JavaScript 的简易 Vue 实现,核心功能包括数据响应式、模板编译和依赖收集。 数据响应式 通过 Object.defineProperty 实…

简单实现vue github

简单实现vue github

实现 Vue 项目并托管到 GitHub 的步骤 创建 Vue 项目 使用 Vue CLI 快速初始化项目,确保已安装 Node.js 和 npm/yarn。 运行命令: npm init vu…

vue实现论坛功能

vue实现论坛功能

实现论坛功能的核心模块 论坛功能通常包含用户发帖、回帖、分类浏览、搜索等核心模块。Vue.js 作为前端框架,需配合后端 API 实现数据交互。 基础项目结构搭建 使用 Vue CLI 或 Vite…

vue实现换页功能

vue实现换页功能

Vue 实现分页功能 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 在 main.js 中引入: import Vue fr…