onMounted hook runs?import { ref, onMounted } from 'vue';
export default {
setup() {
const message = ref('');
onMounted(() => {
message.value = 'Hello from mounted!';
});
return { message };
}
}What will the component display after it is mounted?
onMounted runs after the component is inserted into the DOM.The onMounted hook runs after the component is mounted. It updates the reactive message value, causing the component to re-render and show the new message.
count after onBeforeUpdate runs?import { ref, onBeforeUpdate } from 'vue';
export default {
setup() {
const count = ref(0);
onBeforeUpdate(() => {
count.value += 1;
});
function increment() {
count.value += 1;
}
return { count, increment };
}
}If the user clicks a button that calls
increment once, what will be the value of count after the onBeforeUpdate hook runs?onBeforeUpdate hook runs before the DOM updates but after reactive state changes.When increment is called, count increases from 0 to 1. Then onBeforeUpdate runs and adds 1 more, making count equal to 2.
onUnmounted in Composition API?The correct hook name is onUnmounted and it requires a function argument. Option A uses correct syntax with an arrow function and parentheses.
onUpdated hook cause an infinite loop?import { ref, onUpdated } from 'vue';
export default {
setup() {
const count = ref(0);
onUpdated(() => {
count.value++;
});
return { count };
}
}What causes the infinite update loop?
Modifying reactive state inside onUpdated triggers another update, causing an endless loop of updates.
onMounted runs once after the component is inserted into the DOM. Other hooks like onUpdated and onBeforeUpdate can run many times. onUnmounted runs once but only when the component is removed.
