как перестать повторять мою кнопку лайка внутри моего поста - PullRequest
1 голос
/ 01 апреля 2020

Я работаю в почтовой системе с лайками, где пользователь может переключать посты, как будто я все сделал правильно, кроме последнего шага, проблема внутри моего v-for l oop Я выбираю таблицу лайков из пост-почты. Подобное отношение (многие ко многим, так как в таблице лайков есть user_id и post_id), но она выполняет итерацию моей кнопки, даже когда я добавляю условие, смотрите здесь-> , дублируется как кнопка , я пробовал много вещей v-if, v -шоу, я думаю, что проблема в моем алгоритме. Надеюсь, кто-нибудь сможет это исправить, спасибо.

<div class="panel panel-white post panel-shadow" v-for="post in posts" >
            <div class="post-heading">
                <div class="pull-left image">
                    <img v-bind:src="'img/profile/' + post.user.photo" class="img-circle avatar" alt="user profile image">
                </div>
                <div class="pull-left meta">
                    <div class="title h5">
                        <a href="#"><b>{{post.user.name}}  </b></a>

                        made a post.
                    </div>
                    <h6 class="text-muted time">{{post.created_at | hour}}</h6>
                </div>
            </div>
            <div class="post-description">
                <p>{{post.content}}</p>
                <div class="stats">
                       <button class="btn btn-default stat-item"  @click.prevent="addLike(post.id)">
<i class="fa fa-thumbs-o-up" aria-hidden="false" style="color: blue"  v-for="(like) in post.likes" v-bind:style="like.user_id===id && like.post_id===post.id?'color: blue;':'color: gray;'"  > Like &nbsp;{{post.likes.length}}
</i> <!-- here is the duplicate problem-->
                       </button>
                    <a class="btn btn-default stat-item" @click.prevent>
                        <i class="fa fa-reply-all"> {{post.comments.length}}</i> Comments
                    </a>
                </div>
            </div>
            <comment-input :post="post" :userId="id" :userPhoto="userPhoto"></comment-input>
            <ul class="comments-list" v-for="comment in post.comments?post.comments:''">
                <comments :comment="comment" :userId="id" :userPhoto="userPhoto"></comments>
            </ul>
        </div>
        <hr>

    </div>
</div>

1 Ответ

1 голос
/ 01 апреля 2020

Не л oop через элемент кнопки, попробуйте использовать метод likedBythisUser, чтобы проверить, понравился ли текущий пользователь сообщение, чтобы связать его со стилем кнопки:

methods:{
 likedBythisUser(post,id){
   return post.likes.find(like=>{
          return like.user_id===id && like.post_id===post.id;

   }) // return a boolean value 
  }

}

шаблон:

 <button class="btn btn-default stat-item"  @click.prevent="addLike(post.id)">
<i class="fa fa-thumbs-o-up" aria-hidden="false" style="color: blue" bind:style="likedBythisUser(post,id)?'color: blue;':'color: gray;'"  > Like &nbsp;{{post.likes.length}}
</i> <!-- here is the duplicate problem-->
                       </button>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...