当前位置:首页 > 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 指令实现

vue 指令实现

Vue 指令实现 Vue 指令是 Vue.js 提供的特殊属性,用于在 DOM 元素上添加特殊行为。指令以 v- 前缀开头,例如 v-model、v-if、v-for 等。以下是实现自定义指令和常用内…

vue如何实现

vue如何实现

Vue 实现方法 在 Vue 中实现功能通常涉及组件化开发、状态管理和响应式数据绑定。以下是几种常见的实现方式: 组件化开发 使用 Vue 的单文件组件(.vue 文件)结构,将 UI 拆分为可复用…

vue实现拖拉

vue实现拖拉

实现拖拽功能的基本步骤 在Vue中实现拖拽功能可以通过原生HTML5的拖放API或第三方库如vuedraggable完成。以下是两种方法的详细说明。 使用HTML5原生拖放API HTML5提供了原…

vue滑块实现

vue滑块实现

Vue滑块实现方法 使用原生HTML5 range input Vue中可以绑定原生HTML5的range类型input元素实现基础滑块功能: <template> <div&…

vue实现checkbox

vue实现checkbox

Vue 实现 Checkbox 在 Vue 中实现 Checkbox 可以通过原生 HTML 的 <input type="checkbox"> 或使用 Vue 的 v-model 指令绑…

黑马vue实现

黑马vue实现

以下是关于使用 Vue.js 实现黑马相关功能或项目的关键方法和实践指南: 环境配置与项目初始化 安装最新版 Vue CLI 或使用 Vite 创建项目: npm create vue@lates…