Vuetify 是一个流行的 Vue.js UI 框架,但它本身没有提供无限循环的功能
下面是在 Vue 3 中使用 Vuetify 创建一个简单的无限滚动列表的步骤:
npm install vuetify
main.js
中引入 Vuetify 并使用:import { createApp } from 'vue'
import App from './App.vue'
import vuetify from './plugins/vuetify'
const app = createApp(App)
app.use(vuetify)
app.mount('#app')
<template>
<v-container>
<v-row>
<v-col v-for="(item, index) in items" :key="index" cols="12" sm="6" md="4" lg="3">
<v-card>
<v-card-title>{{ item.text }}</v-card-title>
</v-card>
</v-col>
</v-row>
<div v-intersect="onIntersect" />
</v-container>
</template>
<script>
export default {
data() {
return {
items: [],
page: 1,
loading: false,
};
},
methods: {
async onIntersect(entries, observer, isIntersecting) {
if (isIntersecting && !this.loading) {
this.loading = true;
await this.fetchMoreItems();
this.loading = false;
}
},
async fetchMoreItems() {
// 模拟异步请求数据
await new Promise((resolve) => setTimeout(resolve, 1000));
for (let i = 0; i < 10; i++) {
this.items.push({ text: 'Item ' + ((this.page - 1) * 10 + i + 1) });
}
this.page++;
},
},
mounted() {
this.fetchMoreItems();
},
};
</script>
在这个例子中,我们使用了 v-intersect
指令来检测页面底部的元素是否出现在视口中。当底部的元素可见时,我们触发 onIntersect
方法来加载更多数据。这只是一个简单的例子,您可能需要根据您的实际需求和API进行调整。
领取专属 10元无门槛券
手把手带您无忧上云