javascript - 在 vuejs 中加载更多按钮

标签 javascript vue.js vuejs2

我从 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;
    }
  }
});

我想创建一个按钮来加载更多内容并十个显示评论。

我该怎么做?

enter image description here

最佳答案

没有 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>

希望对您有所帮助!

关于javascript - 在 vuejs 中加载更多按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53246481/

相关文章:

javascript - 从 AngularJS 将复杂对象传递给 WebAPI

javascript - 停止多次点击 vue.js

javascript - 页面中的 Nuxtjs 异步等待在页面刷新时不起作用

javascript - 选择不工作的 VueJS 默认值

javascript - 使用 API key 加载 D3 Url

javascript - Angular 的第三级点表示法?

javascript - 使用 "+"连接/连接模板元素

javascript - 使用 axios 和 nuxt 的服务器和客户端 API 调用的不同 baseURL

javascript - 在 v-for 循环中使用 vue 组件

javascript - 如何判断 D3 强制布局何时停止