简体   繁体   English

在 vuejs 中加载更多按钮

[英]Load more button in vuejs

I receive from php an array with customer reviews:我从 php 收到一个带有客户评论的数组:

var comment_list = new Vue({

el: '#comment-list',

 data: {
    testimonials: JSON.parse('{!! addslashes(json_encode(array_reverse($product_info['testimonials'])))!!}'),
 },

 methods: {
    colorAvatar: function(letter) {
        return 'avatar-' + letter.toLowerCase();
    },
    starClass: function(star) {
        return 'star-' + star;
    }
  }
});

I want to create a button to load more and show comments ten by ten.我想创建一个按钮来加载更多并十个十个显示评论。

How can I do it?我该怎么做?

在此处输入图片说明

Without an API, and loading all the comments on the initial load:没有 API,并在初始加载时加载所有注释:

 new Vue({ el: ".vue", data() { return { reviews: [{name: 'Derek', description: 'Some comment'}, {name: 'Joe', description: 'Some comment'},{name: 'Mike', description: 'Some comment'}, {name: 'Ron', description: 'Some comment'},{name: 'Dii', description: 'Some comment'}, {name: 'Lonnie', description: 'Some comment'},{name: 'Paul', description: 'Some comment'}, {name: 'Mike', description: 'Some comment'},{name: 'Jody', description: 'Some comment'}, {name: 'Ryn', description: 'Some comment'},{name: 'Jord', description: 'Some comment'}, {name: 'Milly', description: 'Some comment'},{name: 'Judy', description: 'Some comment'}, {name: 'Vanilly', description: 'Some comment'},{name: 'Nolan', description: 'Some comment'}, {name: 'Pino', description: 'Some comment'},{name: 'Ryne', description: 'Some comment'}, {name: 'Scott', description: 'Some comment'},{name: 'Son', description: 'Some comment'}, {name: 'Bann', description: 'Some comment'},], commentsToShow: 2 }; } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script> <div class="container vue"> <div v-if="commentIndex <= commentsToShow" v-for="commentIndex in commentsToShow"> <div>{{reviews[commentIndex - 1].name}} says:</div> <i><div>{{reviews[commentIndex - 1].description}}</div></i> <hr /> </div> <button @click="commentsToShow += 2">show more reviews</button> </div>

I hope this helps!我希望这有帮助!

The correct way is using AJAX, and send a request each button click.正确的方法是使用 AJAX,并在每次单击按钮时发送一个请求。

You need to create an web service endpoint (for example /api/comments ) and send the code of this web service send you the comments as JSON.您需要创建一个 Web 服务端点(例如/api/comments )并将此 Web 服务的代码以 JSON 形式发送给您。 You may also add parameter ?page=1 to be able to divide it to tens, page 1 is the first ten, page 2 is the second ten and so on.您也可以添加参数?page=1可以将其分成十位,第 1 页是前十位,第 2 页是后十位,依此类推。

Then, you need to assign on click event handler to the "load more" button, that should sends the request to that web service.然后,您需要将单击事件处理程序分配给“加载更多”按钮,该按钮应该将请求发送到该 Web 服务。 You can use axios here like this:你可以像这样在这里使用axios

axios.get('/api/comments').then(res => {
    /* returned data will be in res.data */
    /* it should contain the comments array you sent in the web service */
    this.testimonials = res.data;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM