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
});

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.

Further reading

Problem with a question

Vue · Middle · question 23

Test yourself

Take the Vue · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz