如何在 Vuejs 和 Vuetify 的延迟加载上延迟加载项目列表?

Pru*_*tty 2 vue.js vuejs2 vuetify.js

我正在尝试制作一个无限滚动列表,但它真的不是懒加载,坚持了几个小时,整个列表都会进来。

      .....
    <v-col
      v-for="(post, i) in posts"
      :key="i"
      cols="12"
    >
    <v-lazy
    v-model="isActive"
    :options="{
      threshold: .5
    }"
    transition="fade-transition"
  >
          {{Content here}}
     </....
Run Code Online (Sandbox Code Playgroud)

用于测试的 API:https : //jsonplaceholder.typicode.com/posts

Zim*_*Zim 8

有一个新virtual-scroller组件,但它不适用于网格行/列等响应式内容。而是使用v-lazy...

我发现列需要定义最小高度(大约为卡片的预期高度),以便v-lazy相交观察者工作。使用类似v-sheet或的东西v-responsive来设置min-height和包含卡片。

此外绑定v-modelv-lazy每一个岗位(即:post.isActive),而不是全局isActive无功...

        <v-col lg="3" md="4" sm="6" cols="12" v-for="(post, index) in posts">
                <v-sheet min-height="250" class="fill-height" color="transparent">
                    <v-lazy 
                        v-model="post.isActive" :options="{
                            threshold: .5
                        }"
                        class="fill-height">
                        <v-card class="fill-height" hover>
                            <v-card-text>
                                <v-row :key="index" @click="">
                                    <v-col sm="10" cols="12" class="text-sm-left text-center">
                                        #{{ (index+1) }}
                                        <h2 v-html="post.title"></h2>
                                        <div v-html="post.body"></div>
                                    </v-col>
                                </v-row>
                            </v-card-text>
                        </v-card>
                    </v-lazy>
                </v-sheet>
        </v-col>

   
Run Code Online (Sandbox Code Playgroud)

演示:https : //codeply.com/p/eOZKk873AJ


小智 5

我可以建议使用 v-intersect 的另一种解决方案,它非常适合我。

抱歉,该片段可能无法按我的代码组成,但想法应该很清楚

<template>
  <v-list class="overflow-y-auto" max-height="500">
    <v-list-item v-for="item in items">
      {{ item.name }}
    </v-list-item>
    <v-skeleton-loader v-if="moreDataToAvailable" v-intersect="loadNextPage" type="list-item@5" />
  </v-list>
</template>

<script lang="ts">
import Vue from 'vue'

const pageSize = 10

export default Vue.extend({
  data(): any {
    return {
      pageLoaded: 0,
      totalCount: 100,//fetch from API
      items: []
    }
  },
  computed: {
    moreDataToAvailable (): boolean {
      return Math.ceil(this.totalCount / pageSize) - 1 > this.pageLoaded
    }
  },
  methods {
    async loadNextPage (entries: IntersectionObserverEntry[]) {
      if (entries[0].isIntersecting && this.moreDataToAvailable) {
        const nextPage = this.pageLoaded + 1
        const loaded = await this.loadPage(nextPage) //API call
        loaded.data.forEach((item: any) => this.items.push(item))
        this.totalCount = loaded.totalCount
        this.pageLoaded = nextPage
      }
    },
  }
})
</script>
Run Code Online (Sandbox Code Playgroud)