Which statement best describes the key difference between Vue's Options API and Composition API?
Think about how code is grouped and reused in each API style.
The Options API groups code by options like data, methods, and computed. The Composition API groups code by logical concerns using functions and reactive references, making it easier to reuse and organize complex logic.
Consider these two Vue components. What is the main difference in how their reactive state is declared and accessed?
Component A (Options API):
export default {
data() {
return { count: 0 };
},
methods: {
increment() {
this.count++;
}
}
}
Component B (Composition API):
import { ref } from 'vue';
export default {
setup() {
const count = ref(0);
function increment() {
count.value++;
}
return { count, increment };
}
}Think about how reactive variables are accessed differently in Options API vs Composition API.
In Options API, reactive state is declared as plain properties inside the data() function and accessed with this.count. In Composition API, reactive state uses ref and must be accessed with count.value.
Which option shows the correct syntax for a Vue 3 Composition API setup function that returns a reactive variable and a method?
Remember how to update a ref's value inside a function.
Option A correctly declares a ref and updates its value using message.value. Option A uses a plain variable which is not reactive. Option A incorrectly uses reactive on a string (should be an object). Option A tries to assign directly to the ref variable instead of its .value.
Why does this Composition API code fail to update the displayed count, while the Options API version works?
Composition API snippet:
setup() {
let count = 0;
function increment() {
count++;
}
return { count, increment };
}Think about what makes a variable reactive in Composition API.
In Composition API, variables must be wrapped in ref() or reactive() to be reactive. Declaring count as a plain number means Vue cannot detect changes, so the UI does not update.
Given this Vue 3 component mixing Options API and Composition API, what will be the final rendered count after calling incrementBoth() once?
export default {
data() {
return { countOptions: 0 };
},
setup() {
const countComposition = ref(0);
function incrementBoth() {
this.countOptions++;
countComposition.value++;
}
return { countComposition, incrementBoth };
}
}Template displays: {{ countOptions }} - {{ countComposition }}
Consider the context of this inside incrementBoth in Composition API.
Inside the setup() function, this is undefined. So this.countOptions++ does not update the Options API state. Only countComposition.value++ updates, so the rendered output is 0 - 1.