Composables and mixins help you reuse code in Vue components. They make your code cleaner and easier to manage.
Composable vs mixin comparison in Vue
/* Mixin example */ export const myMixin = { data() { return { count: 0 }; }, methods: { increment() { this.count++; } } }; /* Composable example */ import { ref } from 'vue'; export function useCounter() { const count = ref(0); function increment() { count.value++; } return { count, increment }; }
Mixins are objects with options like data and methods.
Composables are functions that use Vue's reactive features and return state and functions.
/* Using a mixin in a component */ import { myMixin } from './mixins'; export default { mixins: [myMixin], mounted() { this.increment(); console.log(this.count); } };
/* Using a composable in a component */ import { useCounter } from './composables'; import { defineComponent } from 'vue'; export default defineComponent({ setup() { const { count, increment } = useCounter(); increment(); console.log(count.value); return { count, increment }; } });
This Vue component uses a composable to manage a toggle state. It toggles the state twice and logs the final value.
<script setup> import { ref } from 'vue'; // Composable function function useToggle() { const isOn = ref(false); function toggle() { isOn.value = !isOn.value; } return { isOn, toggle }; } // Using composable const { isOn, toggle } = useToggle(); // Toggle twice toggle(); toggle(); console.log(isOn.value); </script>
Mixins can cause name conflicts if multiple mixins have the same property names.
Composables avoid conflicts by using local variables and returning only what you want.
Composables work better with TypeScript and modern Vue features.
Mixins are older and use Vue options like data and methods.
Composables are functions that use Vue's Composition API for better code reuse.
Prefer composables for clearer, safer, and more flexible code sharing.
Practice
mixin and a composable in Vue?Solution
Step 1: Understand mixin structure
Mixins use Vue's Options API with properties like data, methods, and lifecycle hooks.Step 2: Understand composable structure
Composables are plain functions that use Vue's Composition API to share reactive logic.Final Answer:
Mixins use Vue options like data and methods, while composables are functions using the Composition API. -> Option DQuick Check:
Mixin = Options API, Composable = Composition API [OK]
- Confusing mixins as functions
- Thinking composables use Options API
- Believing mixins and composables are identical
Solution
Step 1: Identify composable function pattern
Composable functions are plain functions that return reactive state or methods, often prefixed with 'use'.Step 2: Check syntax correctness
export function useCounter() { const count = ref(0); return { count }; } defines a function returning a reactive ref, which matches composable pattern.Final Answer:
export function useCounter() { const count = ref(0); return { count }; } -> Option BQuick Check:
Composable = function returning reactive state [OK]
- Using object syntax instead of function
- Not returning reactive state
- Using 'this' inside composable function
const myMixin = {
data() { return { count: 0 }; },
methods: { increment() { this.count++; console.log(this.count); } }
}
export default {
mixins: [myMixin],
template: `Click`
}Solution
Step 1: Understand mixin data and methods
The mixin provides a reactive count starting at 0 and an increment method that increases count and logs it.Step 2: Check component usage
The component uses the mixin, so count and increment are available. Each click calls increment, increasing count by 1 and logging it.Final Answer:
Logs 1, 2, 3 on each click -> Option AQuick Check:
Mixin data + method increments count [OK]
- Assuming count resets each click
- Thinking count is undefined
- Confusing mixin with composable behavior
import { ref } from 'vue';
function useTimer() {
const time = ref(0);
setInterval(() => time.value++, 1000);
return { time };
}
export default {
setup() {
const timer = useTimer;
return { timer };
},
template: `{{ timer.time }}`
}Solution
Step 1: Check composable invocation
The composable useTimer is assigned without parentheses, so timer is a function reference, not the returned object.Step 2: Understand impact on template
Since timer is a function, accessing timer.time is undefined, causing template to fail or show nothing.Final Answer:
The composable function is not called; should be useTimer() -> Option CQuick Check:
Call composable as function: useTimer() [OK]
- Forgetting parentheses when calling composable
- Moving setInterval outside composable unnecessarily
- Misreading template syntax
Choose the best option and explain why.
Solution
Step 1: Evaluate mixins drawbacks
Mixins can cause naming conflicts and make code harder to understand because they merge properties invisibly.Step 2: Evaluate composables benefits
Composables are explicit functions that return reactive state and methods, improving clarity, reusability, and TypeScript support.Step 3: Consider other options
Copy-pasting duplicates code, and global variables break Vue's reactivity system.Final Answer:
Composables share reactive logic clearly and safely with better TypeScript support. -> Option AQuick Check:
Composable = clear, safe, reusable logic [OK]
- Choosing mixins despite conflicts
- Ignoring reactivity with global variables
- Thinking copy-paste is maintainable
Ready to prove it?
Pass the test and this pattern moves unknown
