Vue

vue/computed-pure

A computed getter only derives a value - no async work, no requests and no changes to other state.

umiarkowanastatycznefe-vue

Dlaczego

A computed is a cached derivation: Vue runs it when it likes, as often as it likes, and caches only its return value. An async getter returns a promise, not the data; a request inside it fires at unpredictable times, and setting other state from it causes update loops and hard-to-follow renders.

Poprawka

Keep the getter a pure function of its sources. Load data in a watcher (watch(id, load, { immediate: true })), a data library or the framework's loader (useFetch in Nuxt), store the result in a ref, and derive from that ref in the computed.

Przykłady

// bad
const user = computed(async () => (await fetch(`/api/users/${id.value}`)).json())
// good
const user = ref(null)
watch(id, async (value) => { user.value = await getJson(`/api/users/${value}`) }, { immediate: true })
const name = computed(() => user.value?.name ?? '')

Źródła

Fakty

Identyfikator
vue/computed-pure
Waga
umiarkowana
Sprawdzanie
statyczne sprawdzane w kodzie źródłowym przez audit
Dotyczy plików
vue
Tylko w projektach z
vue nuxt
Uczy jej skill
fe-vue Vue components
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

npx github:dawidolko/frontend-skills explain vue/computed-pure