javascript - 如何在 VueJS 中添加显示而不重新渲染整个组件

标签 javascript php laravel vue.js

我刚刚接触 Laravel 和 Vuejs。我遇到了这个问题,其中当单击“加载更多”按钮或向下滚动到底部时,整个组件正在重新渲染。按钮或滚动条的作用就像分页,但您所能做的就是加载更多或添加更多显示。我的问题是如何在不重新渲染整个组件的情况下渲染新的显示。

我尝试创建一个变量,其中它将传递要显示的分页数。是的,它确实有效,但组件正在重新渲染,并且来自服务器的回复的大小变得越来越大。

这是我的 Vue 组件上的脚本:

<script>
export default {
    props: ['user','review_count'],
    data(){
        return{
            reviews: {},
            limit: 2,
            scrolledToBottom: false,
        }
    },
    created(){
        this.getReviews();
        this.scroll();
    },
    methods: {
        getReviews: function(page){
            axios.get('/datas/reviews?user='+ this.user + '&limit='+ this.limit)
            .then((response) =>{
                this.reviews = response.data.data;
            })
            .catch(()=>{
            });
        },
        countType: function(data, type) {
            return data.filter(function(value) { return value.type === type }).length;
        },
        loadMore: function(){
            this.limit+=6;
            this.getReviews();
        },
        scroll () {
            window.onscroll = () => {
                let bottomOfWindow = Math.max(window.pageYOffset, document.documentElement.scrollTop, document.body.scrollTop) + window.innerHeight === document.documentElement.offsetHeight
                if (bottomOfWindow&&this.review_count>this.limit) {
                    this.loadMore();
                }
            }
        }
    }
}
</script>

这是我的 Controller :

public function reviews()
    {
        if($users = \Request::get('user')){
            if($limit = \Request::get('limit')){
        $reviews = Review::select(\DB::raw('id, product_id, review_rating, review_content'))
                        ->where('user_id', $users)
                        ->with('products:product_image,id,product_name')
                        ->with('activities')
                        ->orderBy('reviews.created_at', 'DESC')
                        ->paginate($limit);}}
        return $reviews;
    }

最佳答案

我刚刚解决了我自己的问题,解决方案是使用分页。每次向下滚动时,我只是将服务器中的所有数据推送到一个对象中。

这是我的向下滚动代码:

scroll () {
            window.onscroll = () => {
                let bottomOfWindow = Math.max(window.pageYOffset, document.documentElement.scrollTop, document.body.scrollTop) + window.innerHeight === document.documentElement.offsetHeight
                if (bottomOfWindow&&this.review_count>this.reviews.length) {
                this.getReviews();
                }
            }
        }

这是我的 getReviews() 代码:

getReviews: function(){
            let vm = this;
            axios.get('/datas/reviews?user='+ this.user + '&page='+ this.page)
            .then((response) =>{
                $.each(response.data.data, function(key, value) {
                                vm.reviews.push(value);
                                console.log((vm.reviews));
                        });
            })
            .catch(()=>{
            });
            this.page+=1;
        },

我想出了这个想法,这样我就不会再使用分页来查看下一篇文章了。它更像是一个无限滚动分页组件。

关于javascript - 如何在 VueJS 中添加显示而不重新渲染整个组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57984797/

相关文章:

node.js - 在 Homestead 上安装 npm 和 node

javascript - 如何在 Bootstrap Material 设计的输入文本中制作标签支持?

javascript - 没有函数参数的不显眼的 javascript

php - 通过PHP在MySQL中插入Jquery

php - MySQL:如果字段为空则赋值

php - 在 Laravel 中创建 mysql 帐户用户和数据库

php - 在 Laravel 中读取 excel 文件

JavaScript 和原型(prototype)设计过程

php - 什么是互联网连接设备的唯一标识 IP/MAC?

php - 来自 Angular 的 $http.post 到达 php,但 Angularjs 中未收到响应