当前位置:首页 > VUE

vue 实现点赞

2026-02-20 06:11:26VUE

实现点赞功能的基本思路

在Vue中实现点赞功能通常涉及前端交互与后端数据同步。核心逻辑包括:按钮状态切换、动画效果、数据持久化(如调用API更新数据库)。以下是具体实现方法:

基础实现(纯前端)

仅展示前端交互效果,不涉及后端数据存储:

vue 实现点赞

<template>
  <button 
    @click="toggleLike" 
    :class="{ 'liked': isLiked }"
  >
    {{ isLiked ? '已赞' : '点赞' }} ({{ likeCount }})
  </button>
</template>

<script>
export default {
  data() {
    return {
      isLiked: false,
      likeCount: 0
    }
  },
  methods: {
    toggleLike() {
      this.isLiked = !this.isLiked;
      this.likeCount += this.isLiked ? 1 : -1;
    }
  }
}
</script>

<style scoped>
.liked {
  color: red;
  font-weight: bold;
}
</style>

完整实现(前后端交互)

假设后端提供POST /api/like接口,需传递文章ID和用户ID:

<template>
  <button 
    @click="handleLike" 
    :disabled="isLoading"
    :class="{ 'liked': isLiked }"
  >
    <span v-if="isLoading">处理中...</span>
    <span v-else>{{ isLiked ? '已赞' : '点赞' }} ({{ likeCount }})</span>
  </button>
</template>

<script>
import axios from 'axios';

export default {
  props: {
    postId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      isLiked: false,
      likeCount: 0,
      isLoading: false
    }
  },
  async created() {
    await this.fetchLikeStatus();
  },
  methods: {
    async fetchLikeStatus() {
      const res = await axios.get(`/api/like/status?postId=${this.postId}`);
      this.isLiked = res.data.isLiked;
      this.likeCount = res.data.count;
    },
    async handleLike() {
      this.isLoading = true;
      try {
        await axios.post('/api/like', {
          postId: this.postId,
          action: this.isLiked ? 'unlike' : 'like'
        });
        this.isLiked = !this.isLiked;
        this.likeCount += this.isLiked ? 1 : -1;
      } catch (error) {
        console.error('操作失败:', error);
      } finally {
        this.isLoading = false;
      }
    }
  }
}
</script>

添加动画效果

使用Vue的过渡动画增强用户体验:

vue 实现点赞

<template>
  <button @click="toggleLike">
    <transition name="bounce">
      <span v-if="isLiked" key="liked">❤️</span>
      <span v-else key="unliked">♡</span>
    </transition>
    {{ likeCount }}
  </button>
</template>

<style scoped>
.bounce-enter-active {
  animation: bounce-in 0.5s;
}
@keyframes bounce-in {
  0% { transform: scale(0.9); }
  50% { transform: scale(1.2); }
  100% { transform: scale(1); }
}
</style>

防抖优化

防止用户快速连续点击导致重复请求:

import _ from 'lodash';

export default {
  methods: {
    handleLike: _.debounce(async function() {
      // 原有逻辑
    }, 500)
  }
}

本地存储记忆

使用localStorage记住用户操作状态:

export default {
  data() {
    return {
      isLiked: localStorage.getItem(`like_${this.postId}`) === 'true'
    }
  },
  methods: {
    toggleLike() {
      this.isLiked = !this.isLiked;
      localStorage.setItem(`like_${this.postId}`, this.isLiked);
    }
  }
}

注意事项

  1. 后端接口需做幂等性处理,防止重复点赞
  2. 对于未登录用户,应禁用按钮并提示登录
  3. 敏感操作需在后端验证用户权限
  4. 高并发场景建议使用Redis计数

以上实现可根据实际需求组合使用,例如同时添加动画效果和前后端交互。

标签: vue
分享给朋友:

相关文章

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert…

vue实现公式

vue实现公式

在Vue中实现公式展示或计算可以通过多种方式实现,以下为具体方法: 使用模板插值与计算属性 若公式较简单,可直接在模板中使用插值表达式或计算属性。例如计算圆的面积: <template&…

vue实现监听

vue实现监听

监听数据变化 在Vue中,可以通过watch选项或$watch方法监听数据的变化。watch适用于组件选项内声明式监听,$watch适用于动态监听。 // 选项式API export defaul…

vue实现tap

vue实现tap

Vue 实现 Tap 事件 在移动端开发中,Tap 事件比 Click 事件更常用,因为 Click 事件有 300ms 延迟。以下是几种在 Vue 中实现 Tap 事件的方法。 使用第三方库 安…

vue搜索实现

vue搜索实现

Vue 搜索功能实现方法 基于计算属性的搜索 在 Vue 中可以通过计算属性实现简单的搜索功能。这种方式适合小型数据集,利用 JavaScript 的 filter 方法进行筛选。 data()…

vue实现影院

vue实现影院

Vue实现影院功能的基本思路 使用Vue框架开发影院功能需要结合前端组件化开发、状态管理以及后端API交互。以下是关键实现步骤: 影院页面布局设计 影院页面通常分为顶部导航栏、影院列表区域和底部导…