【发布时间】:2021-09-10 02:14:22
【问题描述】:
请记住,您应该始终尝试处理状态来控制您的用户界面。
<button v-bind:style='{color: isLiked(post.id)?"red":"white"}'></button>
我认为点赞按钮切换是针对登录用户的。您应该为经过身份验证的用户保留一个likelist。
new Vue({
data:{
posts:[],
likeList:[]
},
methods:{
async AddLike(id){
let post = this.posts.find(post => post.id === id)
await axios.post('/video/api/post/like', {post_id: id})
.then(response => {
console.log(response.data)
let like = response.data;
if(like){
post.post_total_likes +=1
this.likeList.push(id)
}else{
post.post_total_likes -=1
let index = this.likeList.findIndex(item => item === id);
if(index > -1){
this.likeList.splice(index,1);
}
}
})
.catch(error => {
console.log(error)
})
},
isLiked(id){
return this.likelist.includes(id);
}
}
})
【问题讨论】:
-
对不起。它错误地编辑了问题。哦。我如何编辑问题?我不是作者。
-
谢谢你,我会把它还给旧的qustion
标签: vue.js vuejs2 vue-component vuetify.js