【问题标题】:Newly added post takes the likes and comment counts of previous post in vue.js/laravel project新添加的帖子采用 vue.js/laravel 项目中之前帖子的点赞数和评论数
【发布时间】:2018-03-24 11:29:54
【问题描述】:

我目前正在学习 vue.js。但是,我很难尝试在我正在做的项目上调试错误,这是一个类似博客的应用程序,以 Laravel 作为后端。 在我的 home Blade.file 中,我有一个名为 <timeline> 的根组件,其中我有 textarea 来添加帖子,然后是一个名为 <posts> 的组件。在 <posts> 内部,我有 <post> 组件,其中显示了各个帖子的详细信息。 我还有一个组件 <feedback> 用于喜欢和评论计数,所有这些都运行良好。 但是,我的挑战是,每次我添加新帖子时,新帖子都会继承反馈,而不是喜欢计数和评论计数为 0 上一篇文章的。(假设上一篇文章有​​ 10 个赞和 5 个 cmets,一旦我进入一个新帖子,它会立即显示 10 个赞和 5 个评论计数,而不是自新帖子以来的 0,并且还没有)。上一篇文章依次接受前一篇文章的反馈,依此类推。 请问我该如何纠正这个错误? 我的组件如下图:

//Timeline component
<template>
    <div class="timeline">
        <div class="form-group">
            <textarea name="body" class="form-control" v-model="postText" rows="4" placeholder="What's on your mind"></textarea>
        </div>
        <div class="form-group">
            <button type="button" class="btn btn-primary" @click.prevent="sendPost">Post</button>
        </div>
        <hr>
        <posts :posts="posts"></posts>
    </div>
</template>

<script>
    export default {
        data(){
            return {
                postText: '',
                posts: []
            }
        },
        methods: {
            loadPost(){
                axios.get('/post').then(response => {
                    console.log(response.data);
                    this.posts = response.data;
                })
            },
            sendPost(){
                if(!this.postText || this.postText.trim() === ''){
                return;
             }

             //send to db
             axios.post('/post', {
                body: this.postText.trim()
             }).then(response=> {
                console.log(response);
                this.loadPost();
             });
             this.postText = '';
            }
        },
        mounted(){
            this.loadPost();
        }
    }
</script>

//posts component
<template>
    <div class="posts">
        <div v-for="post in posts">
            <post :post="post"></post>
        </div>
    </div>
</template>

<script>
    export default {
        props: ['posts']
    }
</script>

这是我的帖子组件

<template>
    <div class="post">
        <div class="media">
            <a class="pull-left" href="#">
                <img :src="'/images/profile_pix/avatar.png'" class="media-object center-block" alt="#"/>
            </a>
            <div class="media-body">
                <h5><strong> {{ post.user.username }}</strong></h5>
                <small>{{ post.created_at }}</small>
                  <p>{{ post.body }} </p>
                <feedback :post="post"></feedback>
            </div>
        </div><hr>
      </div>
</template>

<script>
    export default {
        props: ['post']
    }
</script>

这是我的反馈组件

<template>
    <div class="feedback">
        <span v-if="liked"><a href="#" @click.prevent="unLikeApost"><i class="fa fa-thumbs-down"></i>Unlike</a></span>
        <span v-else="liked"><a href="#" @click.prevent="likeApost"><i class="fa fa-thumbs-up"></i>Like</a></span>
        <span><small> {{likes}} </small></span>                        
        <span>
          <a href="#"><i class="fa fa-comment"></i></a>&nbsp <small> {{ comments }} </small>
        </span>
        <span v-if="author">
          <a href="#" v-if="author" class="pull-right"><small>Delete</small></a>
        </span> 
    </div>
</template>

<script>
    export default {
      props: ['post'],
      data(){
        return {
          author: false,
          likes: '',
          liked: false,
          comments: ''
        } 
      },
      methods: {
        getLikes(){
          axios.get('/likes/'+this.post.id+'/getlikes').then((response) => {
            if(response.data === 0){
              this.likes = 0;
            }
            this.likes = response.data;
          })
        },
        checkIfLiked(){
          axios.get('/likes/'+ this.post.id +'/checkliked').then((response) => {
            if(response.data === 1){
              this.liked = true;
            }
          })
        },
        likeApost(){
            axios.post('/likes/'+ this.post.id +'/likeapost').then((response) => {
              console.log(response.data);
              this.likes += 1;
              this.liked = true;
            })
        },
        unLikeApost(){
            axios.post('/likes/'+ this.post.id +'/unlikeapost').then((response) => {
              console.log(response.data);
              this.likes -= 1;
              this.liked = false;
            })
        },
        countComments(){
          axios.get('/post/'+ this.post.id +'/countcomments').then((response) => {
            this.comments = response.data;
          })
        }
      },
      mounted() {
        // determine author
        axios.get('/post/' +this.post.id+'/getposter').then((response) => {
          let auth_user = window.Laravel.user.id;
          let author = response.data;
          if (auth_user === author){
            this.author = true;
          }
        });

        this.getLikes();

        this.checkIfLiked();

        this.countComments();
      }
    }
</script>

【问题讨论】:

  • 由于您将帖子作为道具传递给feedback,因此可能是api返回了错误的帖子。这听起来很像数组计数问题。后端代码中是否有某些内容返回了不正确的帖子,或者您确定正确的数据正在到达前端?
  • 谢谢@tom_h。在我看来,正确的帖子是从后端返回的。当我添加一个新帖子时,它会存储在数据库中,然后从数据库中获取帖子并显示出来。但是,新帖子的点赞数和评论数预计为零,但这里没有。之前帖子的点赞数和评论数显示在新帖子下。但是,当我刷新页面时,它应该显示零个赞和 cmets。

标签: laravel vue.js


【解决方案1】:

您在提交新帖子后发出 GET /post 而不是 GET /post/:id,这对我来说有点奇怪。或者只是将创建的响应添加到您现有的数组中。

/edit 忽略以上内容,我看到您只是重新运行获取所有帖子的方法。单数的帖子在那里令人困惑,也许应该是 /posts :)

问题可能与key on v-for有关,请尝试使用您那里的帖子ID。

【讨论】:

  • 谢谢@贾斯汀。我刚刚将组件中的 :post="post" 替换为 :key=post.id ,但行为保持不变。请注意,一旦我刷新页面,就会获取正确的计数并将其呈现为 0。此错误会立即显示,我发布一个条目并显示它。我可能做错了什么?
猜你喜欢
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
  • 2014-05-04
  • 1970-01-01
  • 1970-01-01
  • 2014-01-30
相关资源
最近更新 更多