In daily development, the Nuxt 4 Roadmap and New Architecture is being used more and more frequently. This article systematically explains its usage, principles, and optimization strategies.
Quick Start
In real projects, the usage gets more complex:
javascript
import { ref, computed, watch, onMounted } from "vue";
export default {
setup() {
const count = ref(0);
const doubled = computed(() => count.value * 2);
watch(count, (newVal, oldVal) => {
console.log(`count: ${oldVal} -> ${newVal}`);
});
onMounted(() => {
console.log("Component mounted");
});
return { count, doubled };
},
};
Through this approach, both testability and scalability of the code are improved.
Internal Principles
Here is a complete example:
javascript
import { reactive, toRefs, computed } from "vue";
function useCounter(initial = 0) {
const state = reactive({ count: initial, history: [initial] });
const doubled = computed(() => state.count * 2);
function increment() {
state.count++;
state.history.push(state.count);
}
return { ...toRefs(state), doubled, increment };
}
Pay attention to edge case handling — this is critical in production environments.
Real-World Application
The key is understanding the core logic:
javascript
import { ref, computed, watch, onMounted } from "vue";
export default {
setup() {
const count = ref(0);
const doubled = computed(() => count.value * 2);
watch(count, (newVal, oldVal) => {
console.log(`count: ${oldVal} -> ${newVal}`);
});
onMounted(() => {
console.log("Component mounted");
});
return { count, doubled };
},
};
Performance optimization needs to be tailored to specific scenarios; not every situation requires over-optimization.
Performance Comparison
We can improve it in the following ways:
javascript
import { reactive, toRefs, computed } from 'vue'
function useCounter(initial = 0) {
const state = reactive({ count: initial, history: [initial] })
const doubled = computed(() => state.count * 2)
function increment() {
state.count++
state.history.push(state.count)
}