Skip to content

Commit eacad2e

Browse files
committed
copilot
1 parent 22d84ba commit eacad2e

File tree

5 files changed

+41
-17
lines changed

5 files changed

+41
-17
lines changed

examples/vue-pinia/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
Example of using [Pinia](https://pinia.vuejs.org) with the `vike-vue-pinia` extension.
22

33
This example demonstrates:
4-
- Basic Pinia store usage with SSR
5-
- Client-side state persistence using `pinia-plugin-persistedstate` (counter state persists across page reloads)
4+
- Basic Pinia store usage with SSR (Home page: counters synced between components)
5+
- Client-side state persistence using `pinia-plugin-persistedstate` (About page: counter persists across page reloads)
66
- Using `onCreatePinia` hook to register Pinia plugins
7+
- Todo list with SSR data fetching and store population
8+
9+
> [!NOTE]
10+
> The home page uses a **non-persisted** store to demonstrate SSR without hydration issues. The about page uses a **persisted** store to demonstrate localStorage persistence.
711
812
> [!NOTE]
913
> For more examples, see [Bati](https://batijs.dev) which generates `vike-vue` apps.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<template>
2+
<button type="button" @click="counterStore.increment">Persisted Counter {{ counterStore.count }}</button>
3+
</template>
4+
5+
<script lang="ts" setup>
6+
import { usePersistedCounterStore } from '../stores/usePersistedCounterStore'
7+
8+
const counterStore = usePersistedCounterStore()
9+
</script>
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<template>
22
<h1>About</h1>
3-
<p>Example of using Pinia.</p>
4-
<Counter id="counter-3" />
3+
<p>Example of using Pinia with state persistence.</p>
4+
<p><strong>This counter persists across page reloads (stored in localStorage)</strong></p>
5+
<PersistedCounter id="counter-3" />
56
</template>
67

78
<script lang="ts" setup>
8-
import Counter from '../../components/Counter.vue'
9+
import PersistedCounter from '../../components/PersistedCounter.vue'
910
</script>
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
import { defineStore } from 'pinia'
22
import { ref } from 'vue'
33

4-
export const useCounterStore = defineStore(
5-
'counter',
6-
() => {
7-
const count = ref(0)
4+
export const useCounterStore = defineStore('counter', () => {
5+
const count = ref(0)
86

9-
const increment = () => count.value++
7+
const increment = () => count.value++
108

11-
return { count, increment }
12-
},
13-
{
14-
// Persist state to localStorage - counter persists across page reloads
15-
persist: true,
16-
},
17-
)
9+
return { count, increment }
10+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { defineStore } from 'pinia'
2+
import { ref } from 'vue'
3+
4+
export const usePersistedCounterStore = defineStore(
5+
'persistedCounter',
6+
() => {
7+
const count = ref(0)
8+
9+
const increment = () => count.value++
10+
11+
return { count, increment }
12+
},
13+
{
14+
// Persist state to localStorage - counter persists across page reloads
15+
persist: true,
16+
},
17+
)

0 commit comments

Comments
 (0)