like.js
946 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 点赞操作
import { apiUpdateLikes } from "api/blog";
export default {
computed: {
getLikesNumber({ likeList }) {
return (id, likes) => (likeList.includes(id) ? likes + 1 : likes);
},
getLikesColor({ likeList }) {
return (id) => likeList.includes(id);
},
},
data() {
return {
currentId: "", // 当前id
isLike: false, // 是否点赞
likeList: [],
};
},
methods: {
// 点赞
handleLikes(id) {
if (this.likeList.includes(id)) {
this.isLike = true;
this.likeList.splice(this.likeList.indexOf(id), 1);
} else {
this.isLike = false;
this.likeList.push(id);
}
this.currentId = id;
return apiUpdateLikes({ _id: id, isLike: this.isLike })
.then((res) => {})
.catch((err) => {
console.log(err);
})
.finally(() => {});
},
},
};