状态管理
此示例展示如何使用 `useState` 组合式函数,在组件之间创建响应式且适合 SSR 的共享状态。
app.vue
Open docs<script setup>
const counter = useState('counter', () => Math.round(Math.random() * 1000))
const sameCounter = useState('counter')
</script>
<template>
<NuxtExample dir="features/state-management">
<p>Counter: {{ counter }}</p>
<div class="flex gap-2 my-4">
<UButton @click="counter--">
-
</UButton>
<UButton @click="counter++">
+
</UButton>
</div>
<p>Same Counter: {{ sameCounter }}</p>
</NuxtExample>
</template>