在这篇综合指南中,我们将探索如何使用 Inertia.js v2.0 和 Vue 3 在 Laravel 应用程序中实现无限滚动。我们将涵盖前端和后端实现,特别注意处理全页刷新和维护滚动位置。
无限滚动的实现依赖于三个主要组件:
让我们从一个为博客文章列表实现无限滚动的 Vue 组件开始:
<script setup> import { computed } from 'vue' import { usePage, WhenVisible } from '@inertiajs/vue3' import LoadingSpinner from '@/components/LoadingSpinner.vue' import BlogPostCard from '@/components/BlogPostCard.vue' const page = usePage() const hasFeaturePost = computed(() => !!page.props.featuredPost) const categoryName = computed(() => page.props.category?.name) </script> <template> <div> <h3> Key Frontend Features </h3> <ol> <li><p><strong>WhenVisible Component</strong>: This component from Inertia.js v2.0 automatically triggers a request when the element becomes visible in the viewport.</p></li> <li><p><strong>Pagination Parameters</strong>:<br> </p></li> </ol> <pre class="brush:php;toolbar:false">:params="{ data: { page: page.props.postsPagination.current_page + 1, }, only: ['posts', 'postsPagination'], }"
这是处理常规分页和整页加载场景的 Laravel 控制器实现:
<?php namespace App\Http\Controllers; use App\Models\Post; use App\Models\Category; use Illuminate\Pagination\LengthAwarePaginator; use Inertia\Inertia; class BlogController extends Controller { public function index(?Category $category = null) { return Inertia::render('Blog/Index', [ 'category' => $category, 'featuredPost' => $this->getFeaturedPost(), 'posts' => $this->getPaginatedPosts($category), 'postsPagination' => $this->getPaginatedPosts($category)?->toArray(), ]); } protected function getPaginatedPosts(?Category $category): ?LengthAwarePaginator { $currentPage = request()->input('page', 1); $perPage = request()->input('per_page', 12); $query = Post::query() ->with(['author', 'category']) ->published(); if ($category) { $query->where('category_id', $category->id); } // Apply any additional filters if (request()->has('sort')) { $query->orderBy(request()->input('sort'), request()->input('direction', 'desc')); } else { $query->latest(); } // Handle full page load vs. infinite scroll request if (!request()->header('X-Inertia')) { // Full page load - fetch all pages up to current $allResults = collect(); for ($page = 1; $page <= $currentPage; $page++) { $pageResults = $query->paginate($perPage, ['*'], 'page', $page); $allResults = $allResults->concat($pageResults->items()); } return new LengthAwarePaginator( $allResults, Post::query() ->published() ->when($category, fn($q) => $q->where('category_id', $category->id)) ->count(), $perPage, $currentPage ); } return $query->paginate($perPage); } protected function getFeaturedPost() { return Post::query() ->with(['author', 'category']) ->published() ->featured() ->latest() ->first(); } }
if (!request()->header('X-Inertia')) { // Full page load logic } else { // Regular pagination for infinite scroll }
for ($page = 1; $page <= $currentPage; $page++) { $pageResults = $query->paginate($perPage, ['*'], 'page', $page); $allResults = $allResults->concat($pageResults->items()); }
$query = Post::query() ->with(['author', 'category']) ->published();
使用 Laravel 和 Inertia.js v2.0 实现无限滚动可提供流畅的用户体验,同时保持良好的性能和 SEO 实践。 Vue 3 的 Composition API 和 Inertia.js 的 WhenVisible 组件的结合使其易于实现和维护。
记住:
此实现可以适用于博客文章之外的各种用例,例如产品列表、图片库或任何其他受益于无限滚动的内容。
以上是使用 Laravel、Inertia.js v 和 Vue 3 实现无限滚动的详细内容。更多信息请关注PHP中文网其他相关文章!