← Webalkalmazás-alapok · projekttervezési csomag adatlapja
Nézd meg, miben segít
2. Flexible inputs with
Újrahasználható Vue-működés
Az ismétlődő reaktív logikát jól körülhatárolt függvényekbe szervezi, világos bemenetekkel és automatikus erőforrás-felszabadítással.
vue-composables
Ezt a skillt használó ügynökök
Mikor hasznos?
Akkor használd, ha több komponensben ismétlődik az adatlekérés, a billentyűkezelés vagy a feliratkozások kezelése.
Miben segít neked?
- Előbb megkeresi a meglévő segédeket, és csak valóban közös feladathoz készít új composable-t.
- Változó bemeneteket fogad, és érthetően elválasztja a megfigyelhető állapotot a végrehajtható műveletektől.
- A komponens megszűnésekor leállítja az időzítőket, figyelőket és függő kéréseket.
Például ezt kérheted
Emeld ki a közös keresési logikát egy composable-ba. Kövesse a szűrőváltozásokat, adjon betöltési állapotot és frissítési műveletet, bezáráskor pedig szakítsa meg a kérést. Két független keresőpanel használja a composable-t. Egyik bezárása csak saját kérését szakítsa meg; reaktív szűrőváltás friss keresést indítson régi találat nélkül.
Részletes dokumentáció
Itt a skill eredeti útmutatóját, példáit és ellenőrzési szempontjait olvashatod az eredeti nyelven.
Összecsukás
Composable Design
Build focused, composable use* functions that hide real stateful or side-effect complexity behind a small, predictable TypeScript interface.
1. When to write a composable (and when not to)
- Check existing project composables and
@vueuse/core(if installed) before writing a new one. - Extract a composable when stateful reactive logic or side effects (event listeners, observers, async resource lifecycles, keyboard shortcuts, synced storage) are reused across components or obscure a component's main flow.
- Keep stateless, pure transformations in plain TypeScript utility functions (
formatCurrency,parseQuery), notuse*composables. - Give each composable one clear responsibility (
useDocumentTitle,useDebouncedSearch,usePagination) instead of a monolithicusePageEverything.
2. Flexible inputs with MaybeRefOrGetter
- Accept reactive and plain inputs seamlessly using Vue 3.3+ types:
- Read-only inputs: type as
MaybeRefOrGetter<T>and normalize insidecomputed,watchor effects withtoValue(input). - Mutable ref inputs: type as
MaybeRef<T>and normalize withtoRef(input).
- Read-only inputs: type as
- Take 1–2 required positional arguments and group optional configuration into a typed
options?: UseFeatureOptionsobject with sensible defaults.
3. Return contract and encapsulation
- Return a plain object of refs and functions (
return { items: readonly(items), isLoading: readonly(isLoading), refresh }) so callers can destructure properties without losing reactivity, while still allowing reactive wrapping if needed. - Wrap internal state refs in
readonly()when external callers should only mutate state through the composable's returned actions. - Make side effects and async triggers explicit: document or configure via
immediate?: booleanwhether an async composable fetches on creation or waits for an explicit action call.
4. Lifecycle, cleanup and SSR safety
- Call
onMounted,onUnmounted,onScopeDisposeandinjectsynchronously at the top of the composable before anyawait. - Use
getCurrentScope()+onScopeDispose(cleanup)(oronUnmounted) to release timers,AbortControllers, DOM listeners and subscriptions automatically when the owning component oreffectScopestops. - Guard browser-only globals (
window,document,localStorage,matchMedia) behindonMountedortypeof window !== 'undefined'checks so the composable never crashes during SSR or unit tests. - Never store per-request mutable state at module scope unless the composable is an intentional client-only singleton (and even then, prefer Pinia or
createSharedComposablewhen SSR is not used).
Before you finish
- Existing composables and
@vueuse/corechecked before creating a new one. - Inputs accept
MaybeRefOrGetter<T>normalized viatoValue()where reactivity is useful. - Returns a plain object of refs (with
readonly()on internal state) and explicit action functions. - Listeners, timers and in-flight requests are cleaned up via
onScopeDispose/onUnmounted/onWatcherCleanup.
Csomagban lévő fájlok 2
LICENSE.txtSKILL.md
