Vue · Senior · Question 7 of 75
What goes wrong when a lifecycle hook is registered after await in a plain setup() function?
export default {
async setup() {
const data = await load();
onMounted(() => init(data)); // problem
},
};- ✓Correct answer: After
awaitthe current instance is lost, so the hook isn't registered (Vue warns);<script setup>restores it automatically - 2Nothing; hooks can be registered at any time
- 3The component is mounted twice
Explanation
Hooks, watch and inject rely on the active instance, which is only set during synchronous setup. Register them before the first await, or use <script setup>, whose compiler preserves the context.