vue实现静态页面点赞和取消点赞的功能

效果如下:
点击之后 点赞数量+1,红心亮
再次点击,点赞数量-1,红心灭
在这里插入图片描述
在这里插入图片描述
逻辑:
由于列表是动态渲染的(for),数据是mock随机生成,所以绑定点击事件时,应该把当前下标和item的id都传到事件上,在data里面声明空数组,用来存放已经点击的id,
点赞点击事件触发,先进行判断,
1.当前data内的数组是否有这个点击的id,用indexof方法查找,如果找不到,执行点赞功能,数量+1,红心样式取反,最重要的是将当前点赞的id存到data的数组里 push进去。
2.反之找到了,就将他数量-1,心取消。
for遍历data的数组,目的是为了找到当前点击的id的下标,找到后,直接利用splice删除的放法,splice(i,1)第一个参数为下标,第二个删除一个,vue组件代码如下:

<template>
  <div v-if="foodMeishi">
    <div
      class="food-box-content"
      v-for="(item, index) in foodMeishi"
      :key="item.id"
    >
      <img class="food-photo" :src="item.foodphotoUrl" alt="" />
      <div class="head">
        <img :src="item.headImg" alt="" />
        <p class="head-name">{{ item.headName }}</p>
      </div>
      <div class="food-content">
        {{ item.content }}
      </div>
      <div class="food-bottom">
        <div class="xin" @click="dianzan(index, item.id)">
          <i class="iconfont " v-if="item.xin">&#xe607;</i>
          <i class="iconfont" v-else>&#xe68b;</i>
          <span>{{ item.dianzan }}</span>
        </div>
        <div class="pinglun">
          <i class="iconfont">&#xe603;</i>
          <span>{{ item.pinglun }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: ["foodMeishi"],
  data() {
    return {
      zanListId: [1, 2],
    };
  },
  methods: {
    dianzan(index, id) {
      let list = this.zanListId;
      if (list.indexOf(id) == -1) {
        // 没找到
        // 执行点赞功能
        this.foodMeishi[index].dianzan += 1;
        // 加到数组中
        this.zanListId.push(id);
        this.foodMeishi[index].xin = !this.foodMeishi[index].xin;
      } else {
        // 取消点赞
        this.foodMeishi[index].dianzan -= 1;
        this.foodMeishi[index].xin = !this.foodMeishi[index].xin;
        for (var i in this.zanListId) {
          if (this.zanListId[i] == id) {
            this.zanListId.splice(i, 1);
          }
        }
      }
    },
  },
};
</script>

  • 14
    点赞
  • 80
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
实现点赞功能需要前端和后端的协同工作,具体实现步骤如下: 1. 前端页面实现点赞按钮,点击按钮向后端发送请求。 2. 后端接收到请求后,判断该用户是否已经点过赞,如果没有点过则将点赞记录保存到数据库中,否则返回已经点过赞的提示信息。 3. 前端接收到后端的响应后,根据响应状态进行相应的处理,如更新点赞数、禁用点赞按钮等。 下面是一个简单的实现思路: 1. 前端代码: ```html <template> <div> <button @click="likePost">点赞</button> <p>{{ likeCount }}人已点赞</p> </div> </template> <script> export default { data() { return { postId: 1, // 文章ID likeCount: 0, // 点赞数 isLiked: false // 是否已经点赞 }; }, methods: { async likePost() { if (this.isLiked) return; // 如果已经点过赞,则不再发送请求 const res = await this.$axios.post("/like", { postId: this.postId }); if (res.data.code === 0) { this.isLiked = true; this.likeCount++; } else { alert(res.data.msg); } } }, mounted() { // 获取点赞数和是否已经点赞的状态 this.$axios.get("/like", { params: { postId: this.postId } }).then(res => { if (res.data.code === 0) { this.likeCount = res.data.data.likeCount; this.isLiked = res.data.data.isLiked; } }); } }; </script> ``` 2. 后端代码: ```java @RestController public class LikeController { @Autowired private LikeService likeService; @PostMapping("/like") public Result likePost(@RequestBody Map<String, Object> params) { Integer postId = (Integer) params.get("postId"); Integer userId = 1; // 假设当前用户ID为1 boolean isLiked = likeService.isLiked(postId, userId); if (isLiked) { return Result.fail("您已经点过赞了"); } likeService.saveLike(postId, userId); return Result.ok(); } @GetMapping("/like") public Result getLikeInfo(Integer postId) { Integer userId = 1; // 假设当前用户ID为1 LikeStatusDto likeStatus = likeService.getLikeStatus(postId, userId); return Result.ok(likeStatus); } } @Service public class LikeServiceImpl implements LikeService { @Autowired private LikeMapper likeMapper; @Override public boolean isLiked(Integer postId, Integer userId) { Like like = likeMapper.selectByPostIdAndUserId(postId, userId); return like != null; } @Override public void saveLike(Integer postId, Integer userId) { Like like = new Like(); like.setPostId(postId); like.setUserId(userId); likeMapper.insert(like); } @Override public LikeStatusDto getLikeStatus(Integer postId, Integer userId) { int likeCount = likeMapper.countByPostId(postId); boolean isLiked = isLiked(postId, userId); LikeStatusDto likeStatus = new LikeStatusDto(); likeStatus.setLikeCount(likeCount); likeStatus.setLiked(isLiked); return likeStatus; } } ``` 其中,`Like`类是一个简单的实体类,包含文章ID和用户ID两个属性;`LikeMapper`是用于操作数据库的Mapper类;`LikeService`是用于处理业务逻辑的Service类;`LikeStatusDto`是用于封装点赞数和是否已经点赞的状态的DTO类。 上述代码中,我们使用了`@PostMapping`和`@GetMapping`注解来处理前端发送的POST和GET请求,同时也使用了SpringBoot提供的注解来注入依赖、处理异常等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百事可口

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值