The Composition API was created to make Vue components easier to organize and reuse. It helps manage complex logic by grouping related code together.
Why the Composition API exists in Vue
import { ref, computed } from 'vue'; export default { setup() { const count = ref(0); const double = computed(() => count.value * 2); function increment() { count.value++; } return { count, double, increment }; } }
The setup function is the main place to write logic in the Composition API.
Use ref to create reactive data, and computed for derived values.
ref.import { ref } from 'vue'; export default { setup() { const message = ref('Hello!'); return { message }; } }
computed to create a value based on reactive data.import { ref, computed } from 'vue'; export default { setup() { const count = ref(1); const squared = computed(() => count.value * count.value); return { count, squared }; } }
setup.import { ref } from 'vue'; export default { setup() { const isVisible = ref(true); function toggle() { isVisible.value = !isVisible.value; } return { isVisible, toggle }; } }
This Vue component uses the Composition API to manage a counter. The count is reactive, and double automatically updates when count changes. Clicking the button increases the count.
<template>
<div>
<p>Count: {{ count }}</p>
<p>Double: {{ double }}</p>
<button @click="increment">Add 1</button>
</div>
</template>
<script setup>
import { ref, computed } from 'vue';
const count = ref(0);
const double = computed(() => count.value * 2);
function increment() {
count.value++;
}
</script>The Composition API helps keep related code close together, unlike the Options API which separates data, methods, and lifecycle hooks.
It works well with TypeScript because it uses plain JavaScript functions.
You can gradually adopt the Composition API in existing Vue projects.
The Composition API organizes component logic by grouping related code.
It makes code easier to read, reuse, and maintain.
It improves TypeScript support and flexibility in Vue apps.
Practice
Solution
Step 1: Understand Vue component logic challenges
Before Composition API, logic was scattered in options like data, methods, computed, making reuse and reading harder.Step 2: Identify Composition API purpose
The Composition API groups related logic together, improving readability and reuse.Final Answer:
To organize component logic by grouping related code -> Option CQuick Check:
Composition API = Organize logic [OK]
- Thinking it removes templates
- Believing it disables reactivity
- Assuming it breaks TypeScript support
ref function from Vue's Composition API?Solution
Step 1: Recall ES module import syntax in Vue 3+
Vue exports Composition API functions as named exports, so use curly braces for import.Step 2: Check each option
import { ref } from 'vue'; uses named import correctly. import ref from 'vue'; tries default import which is wrong. const ref = require('vue'); uses CommonJS require which is outdated. import { Ref } from 'vue'; imports 'Ref' (capitalized) which is a type, not a function.Final Answer:
import { ref } from 'vue'; -> Option BQuick Check:
Named import with curly braces = import { ref } from 'vue'; [OK]
- Using default import instead of named import
- Using require() instead of import
- Confusing types with functions
setup()?
import { ref } from 'vue';
export default {
setup() {
const count = ref(0);
count.value++;
return { count };
}
}
What is the value of count when rendered?Solution
Step 1: Understand ref and its value property
Theref(0)creates a reactive object with initial value 0 accessible viacount.value.Step 2: Analyze the increment operation
count.value++increases the value from 0 to 1 before returning it.Final Answer:
1 -> Option DQuick Check:
ref value incremented once = 1 [OK]
- Expecting count to be 0 without increment
- Trying to use count directly without .value
- Thinking ref returns a primitive, not an object
import { reactive } from 'vue';
export default {
setup() {
const state = reactive({ count: 0 });
function increment() {
state.count += 1
}
return { state, increment };
}
}Solution
Step 1: Review reactive usage
Thereactivefunction creates a reactive object whose properties can be mutated directly.Step 2: Check the increment function
The function incrementsstate.countcorrectly without syntax errors.Final Answer:
No error; code works correctly -> Option AQuick Check:
Reactive objects allow direct property mutation [OK]
- Thinking reactive properties are immutable
- Confusing reactive with ref usage
- Assuming missing semicolon causes error
Solution
Step 1: Understand logic reuse challenges in Options API
Options API scatters logic in component options, making reuse hard without mixins or plugins.Step 2: Recognize Composition API's reusable functions
Composition API lets you write functions (composables) that return reactive state and methods to share logic easily.Final Answer:
By allowing you to write a reusable function that returns reactive state and methods -> Option AQuick Check:
Composition API enables reusable logic functions [OK]
- Thinking duplication is necessary
- Believing global variables are required
- Assuming reactive data can't be reused
Ready to prove it?
Pass the test and this pattern moves unknown
