← Zurück zu Team für Laravel und Vue
Entdecke, wie der Skill hilft
4. Lazy loading, scroll behavior and
Vue-Navigation und teilbare Ansichten
Verbindet Routen, Filter und Browserverlauf, damit Links die gewünschte Ansicht öffnen und Navigation nachvollziehbar bleibt.
vue-router
Teammitglieder mit diesem Skill
Wann ist der Skill hilfreich?
Nutze diesen Skill, wenn Neuladen Filter verliert, die Zurück-Taste überrascht oder nach einem Datensatzwechsel alte Inhalte stehen bleiben.
Wobei hilft er dir?
- Bewahrt teilbare Suche, Sortierung und Seitennavigation in sicher ausgewerteten URL-Parametern.
- Aktualisiert wiederverwendete Seitenkomponenten bei Parameterwechseln und behandelt überholte Anfragen.
- Schützt ungespeicherte Arbeit und trennt Navigationskomfort von serverseitiger Berechtigungsprüfung.
Das könntest du fragen
Baue eine Kundenliste mit Filtern, die Neuladen und Teilen überstehen. Kundenwechsel laden den neuen Datensatz; beim Verlassen ungespeicherter Änderungen erscheint eine klare Entscheidung. Öffne eine gefilterte URL in anderer Sitzung und wechsle Kundenkennungen. Erhalte URL-Zustand, stoppe überholte Ladevorgänge und bestätige das Verlassen ungespeicherter Entwürfe.
Ausführliche Dokumentation
Hier findest du die ursprüngliche Anleitung, Beispiele und Prüfschritte des Skills in ihrer Originalsprache.
Weniger anzeigen
Vue Router & URL State
Treat the URL as the authoritative source of truth for shareable view state, and handle route transitions, reused component instances and navigation guards predictably.
1. URL state vs component state
- Put state in the URL (
route.params/route.query) when the user expects reload persistence, browser back/forward navigation or a shareable link: active resource IDs, search queries, filter chips, sort order, pagination page and deep-linked tabs. - Derive typed filter/pagination values from
useRoute().queryviacomputed(), handling missing, array or malformed query values safely with defaults. - Update filter and search query params with
router.replace({ query: ... })when typing or toggling filters so every keystroke does not clutter browser history, androuter.push({ query: ... })for distinct navigation steps.
2. Reused component instances on param changes
- Vue Router reuses the same component instance when navigating between routes that render the same component (for example,
/users/1to/users/2).onMountedwill not fire again! - Always react to parameter or query changes either by:
- Watching
() => route.params.id(withimmediate: trueand stale-request cancellation), - Using
onBeforeRouteUpdate(async (to, from) => { ... }), or - Putting
:key="route.path"on<RouterView>only when a full component remount and state reset is genuinely desired.
- Watching
- Prefer
props: trueon route definitions when passing route params into page components so components remain decoupled and easy to test.
3. Navigation guards and unsaved changes
- Use Vue Router 4's return-based guard syntax (
return false,return { name: 'login', query: { redirect: to.fullPath } }, orreturn true) instead of the legacy 3-argumentnext()callback. - When using Pinia inside
router.beforeEach((to) => { ... }), always calluseAuthStore()inside the guard function body, never at module top level beforeapp.use(pinia). - Remember that client-side route guards improve UX (redirecting unauthenticated users early) but are never a security boundary — API endpoints and server pages must enforce authorization independently.
- Protect dirty forms from accidental navigation with
onBeforeRouteLeave((to, from) => { if (isDirty.value && !confirm(...)) return false })alongside abeforeunloadlistener for tab closes.
4. Lazy loading, scroll behavior and <RouterView> transitions
- Lazy-load non-critical route components with dynamic imports (
component: () => import('./views/SettingsView.vue')) so initial bundles stay lean. - When wrapping
<RouterView>in<Transition>or<KeepAlive>, always use thev-slot="{ Component, route }"scoped slot pattern:
and ensure any child component inside<RouterView v-slot="{ Component }"> <KeepAlive><component :is="Component" /></KeepAlive> </RouterView><Transition>has a single root element.
Before you finish
- Shareable view state (filters, search, sort, page, tab) lives in the URL with safe parsing and sensible
pushvsreplacehistory behavior. - Parameter changes on the same route component update state cleanly without relying on
onMountedre-running. - Guards return route locations instead of calling
next(), and dirty forms warn on route leave.
Enthaltene Dateien 2
LICENSE.txtSKILL.md
