【问题标题】:How to increase like count of unique button inside v-for in Vue js如何在Vue js中增加v-for中唯一按钮的数量
【发布时间】: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


【解决方案1】:
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
          }else{
            post.post_total_likes -=1
          }
             
        })
        .catch(error => {
            console.log(error)
        })
      
    }

【讨论】:

    猜你喜欢
    • 2019-11-10
    • 2021-04-16
    • 2018-02-09
    • 2018-12-29
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    相关资源
    最近更新 更多