Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/computed-no-parameters.md
1---2title: Computed Properties Cannot Accept Parameters3impact: MEDIUM4impactDescription: Attempting to pass arguments to computed properties fails or defeats caching5type: capability6tags: [vue3, computed, methods, parameters, common-mistake]7---89# Computed Properties Cannot Accept Parameters1011**Impact: MEDIUM** - Computed properties are designed to derive values from reactive state without parameters. Attempting to pass arguments defeats the caching mechanism or causes errors. Use methods or computed properties that return functions instead.1213## Task Checklist1415- [ ] Use methods when you need to pass parameters16- [ ] Consider if the parameter can be reactive state instead17- [ ] If you must parameterize, understand that returning a function loses caching benefits18- [ ] Prefer method calls in templates for parameterized operations1920**Incorrect:**21```vue22<template>23<!-- BAD: Computed properties don't accept parameters like this -->24<p>{{ filteredItems('active') }}</p>25<p>{{ formattedPrice(100, 'USD') }}</p>26</template>2728<script setup>29import { ref, computed } from 'vue'3031const items = ref([/* ... */])3233// BAD: This won't work as expected34// Computed is called once, not per parameter35const filteredItems = computed((status) => { // status will be undefined or previous value36return items.value.filter(i => i.status === status)37})38</script>39```4041```vue42<script>43export default {44data() {45return { items: [/* ... */] }46},47computed: {48// BAD: Computed doesn't receive arguments49filteredItems(status) { // 'status' is actually 'this' or undefined50return this.items.filter(i => i.status === status)51}52}53}54</script>55```5657**Correct:**58```vue59<template>60<!-- GOOD: Use method for parameterized operations -->61<p>{{ getFilteredItems('active') }}</p>62<p>{{ formatPrice(100, 'USD') }}</p>6364<!-- GOOD: Or use computed with reactive filter state -->65<select v-model="statusFilter">66<option value="active">Active</option>67<option value="inactive">Inactive</option>68</select>69<p>{{ filteredItems }}</p>70</template>7172<script setup>73import { ref, computed } from 'vue'7475const items = ref([/* ... */])76const statusFilter = ref('active')7778// GOOD: Method for parameterized operations79function getFilteredItems(status) {80return items.value.filter(i => i.status === status)81}8283function formatPrice(amount, currency) {84return new Intl.NumberFormat('en-US', {85style: 'currency',86currency87}).format(amount)88}8990// GOOD: Computed with reactive parameter91const filteredItems = computed(() => {92return items.value.filter(i => i.status === statusFilter.value)93})94</script>95```9697## Workaround: Computed Returning a Function9899If you need something computed-like with parameters, you can return a function. **However, this defeats the caching benefit:**100101```vue102<template>103<p>{{ getItemsByStatus('active') }}</p>104</template>105106<script setup>107import { ref, computed } from 'vue'108109const items = ref([/* ... */])110111// This works but provides NO caching benefit112// The inner function runs every time it's called113const getItemsByStatus = computed(() => {114return (status) => items.value.filter(i => i.status === status)115})116117// This is essentially equivalent to just using a method118// Only useful if you need to compose with other computed properties119</script>120```121122## When to Use Each Approach123124| Scenario | Approach | Caching |125|----------|----------|---------|126| Fixed filter based on reactive state | Computed | Yes |127| Dynamic filter passed as argument | Method | No |128| Filter options from user selection | Computed + reactive param | Yes |129| Formatting with variable parameters | Method | No |130| Composed derivation with argument | Computed returning function | Partial |131132## Make Parameters Reactive133134The best pattern is often to make the "parameter" a reactive value:135136```vue137<script setup>138import { ref, computed } from 'vue'139140const items = ref([/* ... */])141142// Instead of passing 'status' as a parameter:143const currentStatus = ref('active')144145// Make a computed that uses the reactive status146const filteredItems = computed(() => {147return items.value.filter(i => i.status === currentStatus.value)148})149150// Change the filter by updating the ref151function filterByStatus(status) {152currentStatus.value = status153}154</script>155```156157## Reference158- [Vue.js Computed Properties](https://vuejs.org/guide/essentials/computed.html)159- [Vue.js Methods](https://vuejs.org/guide/essentials/reactivity-fundamentals.html#declaring-methods)160