Can someone tell me why the page won't load more data when user scrolls to the bottom of the viewport? It doesn't seem to work in Nuxt3, but it works fine in Vue 3. Why?
I am using the useInfiniteScroll
composable from VueUse like so in a Nuxt3 app:
<script setup lang="ts">
import { useInfiniteScroll } from '@vueuse/core';
const el = ref<HTMLElement>(null);
const data = ref([]);
const total = ref(0);
useInfiniteScroll(el, loadData, { distance: 10 });
loadData();
async function loadData() {
if (data.value.length !== 0 && data.value.length >= total.value) return;
const res = await fetch(
`https://dummyjson.com/products/?limit=10&skip=${data.value.length}`
);
const d = await res.json();
total.value = d.total;
data.value = data.value.concat(d.products);
}
</script>
<template>
<div ref="el" class="el h-100">
<div v-for="item in data" class="card">
<img :src="item.thumbnail" alt="" />
<h3>{{ item.title }}</h3>
<p>{{ item.description }}</p>
</div>
</div>
</template>
Here's a full Nuxt3 reproduction here: https://stackblitz.com/edit/nuxt-starter-buzm4q?file=pages/index.vue
A working Vue3 version is here: https://stackblitz.com/edit/vitejs-vite-9i25xx?file=src/App.vue