Vue · Middle · Question 23 of 75
Why must composables be called synchronously in <script setup> rather than in a callback?
onMounted(() => {
const { x } = useMouse(); // problem
});- ✓Correct answer: Vue needs the active component instance to register the composable's lifecycle hooks and watchers
- 2Composables can only be called once per app
- 3Callbacks run on the server only
Explanation
Inside a callback there is no current instance, so onUnmounted inside the composable isn't registered and listeners leak. <script setup> restores the instance after a top-level await, so calls after it still work.