Consider this composable and component using it:
import { ref } from 'vue';
export function useCounter() {
const count = ref(0);
function increment() {
count.value++;
}
return { count, increment };
}
// In a component setup:
const { count, increment } = useCounter();
increment();
increment();
What is the value of count.value after these calls?
Each call to increment() increases count.value by 1.
The composable useCounter returns a reactive count starting at 0. Calling increment() twice increases it to 2.
Choose the correct composable function that returns a reactive string message initialized to "Hello".
Use ref for primitive reactive values like strings.
ref creates a reactive primitive value. reactive is for objects. Option D correctly uses ref and returns an object containing message.
Look at this composable:
import { ref } from 'vue';
const count = ref(0);
export function useSharedCounter() {
function increment() {
count.value++;
}
return { count, increment };
}When used in multiple components, why do they share the same count value?
Think about where variables are declared in JavaScript modules.
Variables declared outside the composable function are shared across all imports and calls, causing shared state. To avoid this, declare reactive state inside the function.
Given this composable and usage:
import { ref, watchEffect } from 'vue';
export function useDoubleCounter() {
const count = ref(1);
const double = ref(0);
watchEffect(() => {
double.value = count.value * 2;
});
function increment() {
count.value++;
}
return { count, double, increment };
}
// In component setup:
const { count, double, increment } = useDoubleCounter();
increment();
increment();
What are the final values of count.value and double.value?
watchEffect updates double whenever count changes.
Starting at 1, two increments make count 3. The watchEffect sets double to twice count, so 6.
Which reason best explains why Vue composables are preferred over mixins for sharing reusable logic?
Think about clarity and code safety when reusing logic.
Composables use explicit function calls returning reactive state, improving type safety and avoiding naming collisions common in mixins.