当前位置:首页 > VUE

vue实现点赞功能

2026-02-24 00:31:34VUE

实现点赞功能的基本思路

在Vue中实现点赞功能通常涉及前端交互与后端数据同步。核心逻辑包括:点击按钮切换状态、更新UI、发送请求到后端保存数据。

前端实现步骤

模板部分

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

脚本部分

vue实现点赞功能

<script>
export default {
  data() {
    return {
      isLiked: false, // 当前用户是否点赞
      likeCount: 0    // 总点赞数
    }
  },
  methods: {
    handleLike() {
      this.isLiked = !this.isLiked;
      this.likeCount += this.isLiked ? 1 : -1;

      // 发送API请求到后端
      axios.post('/api/like', {
        action: this.isLiked ? 'like' : 'unlike'
      }).catch(error => {
        // 失败时回滚状态
        this.isLiked = !this.isLiked;
        this.likeCount += this.isLiked ? -1 : 1;
      });
    }
  }
}
</script>

样式部分

<style scoped>
button {
  padding: 8px 16px;
  background: #f0f0f0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.liked {
  background: #1890ff;
  color: white;
}
</style>

后端交互注意事项

  • 使用防抖避免重复提交(如lodash的_.debounce
  • 重要操作需在后端验证用户权限
  • 考虑使用乐观更新(先更新UI再确认请求成功)

高级优化方案

本地存储记录点赞状态

vue实现点赞功能

// 防止页面刷新后状态丢失
localStorage.setItem('liked_post_123', true);

动画效果增强体验

.liked {
  transition: all 0.3s ease;
  transform: scale(1.1);
}

服务端渲染(SSR)兼容 需在created钩子中初始化状态:

created() {
  if (process.server) {
    this.isLiked = this.$ssrContext.userLikedStatus || false;
  }
}

完整组件示例

<template>
  <button 
    @click="debouncedLike"
    :class="['like-btn', { 'liked': isLiked }]"
    :disabled="isLoading"
  >
    <span v-if="!isLoading">
      {{ isLiked ? '❤️ 已赞' : '🤍 点赞' }} ({{ likeCount }})
    </span>
    <span v-else>加载中...</span>
  </button>
</template>

<script>
import { debounce } from 'lodash';

export default {
  props: {
    initialLiked: Boolean,
    initialCount: Number,
    postId: String
  },
  data() {
    return {
      isLiked: this.initialLiked,
      likeCount: this.initialCount,
      isLoading: false
    };
  },
  created() {
    this.debouncedLike = debounce(this.handleLike, 300);
  },
  methods: {
    async handleLike() {
      this.isLoading = true;
      try {
        const action = this.isLiked ? 'unlike' : 'like';
        await axios.post(`/posts/${this.postId}/like`, { action });

        this.isLiked = !this.isLiked;
        this.likeCount += this.isLiked ? 1 : -1;
        localStorage.setItem(`liked_${this.postId}`, this.isLiked);
      } catch (error) {
        console.error('点赞操作失败', error);
      } finally {
        this.isLoading = false;
      }
    }
  }
};
</script>

标签: 功能vue
分享给朋友:

相关文章

vue实现tablegrid

vue实现tablegrid

Vue 实现 TableGrid 的方法 使用 Element UI 的 Table 组件 Element UI 提供了强大的 Table 组件,可以快速实现表格布局。安装 Element UI 后,…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,…

vue实现suspense

vue实现suspense

Vue 3 中实现 Suspense Suspense 是 Vue 3 引入的一个内置组件,用于处理异步依赖的加载状态。它允许在等待异步组件或异步数据加载时显示一个后备内容(fallback),直到异…

vue mvvm实现

vue mvvm实现

Vue MVVM 实现原理 Vue.js 是一个基于 MVVM(Model-View-ViewModel)模式的框架,其核心是通过数据绑定和响应式系统实现视图与数据的自动同步。以下是 Vue 实现 M…

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,使…

vue实现treeview

vue实现treeview

Vue 实现 TreeView 的方法 使用递归组件实现 TreeView 递归组件是 Vue 中实现 TreeView 的常见方式。通过组件调用自身,可以轻松处理嵌套的树形结构数据。 <t…