Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

State Management and Data Persistence

State Management and Data Persistence

The previous chapter completed componentization; now we face the last hands-on pain point:Page refresh, data lost.

To solve this, we will introduce Vue's officially recommended state management library Pinia, and combine it with Tailwind v4 's styling capability to achieve datapersistent storage.


Pinia State Management and Data Persistence

Pinia is a lightweight state management library for Vue.js that lets you share and manage state between components. We can think of Pinia as a global data warehouse from which all components can fetch or update data.

Pinia related references:Pinia Getting Started Tutorial.

Why use Pinia?

In the previous code, the data was all in App.vue . If the project grows (e.g., adding a statistics page, user center), passing data between components becomes a nesting nightmare (Props Hell).

Installation and initialization:

npm install pinia

npm install @vue/devtools-kit -D

Register in src/main.js:

Example

import

{

createApp

}

from

'vue'

import

{

createPinia

}

from

'pinia'

import

App from

'./App.vue'

import

'./style.css'

const

app

=

createApp

(

App

)

app.

use

(

createPinia

(

)

)

// plugin registration

app.

mount

(

'#app'

)

Create Task Store (warehouse)

Create a stores directory under src, then create taskStore.js under src/stores.

We will move all the logic originally in App.vue over here.

Example

import

{

defineStore

}

from

'pinia'

import

{

ref

,

computed

,

watch

}

from

'vue'

export

const

useTaskStore

=

defineStore

(

'task-store'

,

(

)

=>

{

 

// --- 1. State ---

 

// try to read the initial value from LocalStorage

 

const

savedTasks

=

localStorage.

getItem

(

'my-tasks'

)

 

const

tasks

=

ref

(

savedTasks

?

JSON.

parse

(

savedTasks

)

:

[

]

)

 

const

filter

=

ref

(

'all'

)

 

// --- 2. Derived State (Getters) ---

 

const

filteredTasks

=

computed

(

(

)

=>

{

   

if

(

filter.

value

===

'active'

)

return

tasks.

value

.

filter

(

t

=>

!

t.

isCompleted

)

   

if

(

filter.

value

===

'completed'

)

return

tasks.

value

.

filter

(

t

=>

t.

isCompleted

)

   

return

tasks.

value

 

}

)

 

// --- 3. Actions ---

 

const

addTask

=

(

title

)

=>

{

    tasks.

value

.

unshift

(

{

id

:

crypto.

randomUUID

(

)

,

title

,

isCompleted

:

false

}

)

 

}

 

const

removeTask

=

(

id

)

=>

{

    tasks.

value

=

tasks.

value

.

filter

(

t

=>

t.

id

!==

id

)

 

}

 

const

toggleTask

=

(

id

)

=>

{

   

const

task

=

tasks.

value

.

find

(

t

=>

t.

id

===

id

)

   

if

(

task

)

task.

isCompleted

=

!

task.

isCompleted

 

}

 

// --- 4. Persistence ---

 

// watch tasks for changes, and write to LocalStorage once changed

  watch

(

tasks

,

(

newVal

)

=>

{

    localStorage.

setItem

(

'my-tasks'

,

JSON.

stringify

(

newVal

)

)

 

}

,

{

deep

:

true

}

)

// deeply watch changes to objects inside the array

 

return

{

    tasks

,

    filter

,

    filteredTasks

,

    addTask

,

    removeTask

,

    toggleTask

 

}

}

)

Refactor App.vue (connect to the warehouse)

The current App.vue no longer needs to manage data itself; it just calls the Store.

Example


Detailed Knowledge Points

1. storeToRefs What is it?

If you write it directly like this:const { filter } = taskStore, you will lose reactivity. When you modify filter , the page will not refresh.

2. watch 's deep watching (deep: true)

In the persistence logic, we are watching the tasks array.

3. Persistence approach

We adopted a closed loop of "read on init + write on change":

  1. Read: The Store reads from localStorage on creation to give the application "memory".
  2. Write: Uses watch to achieve fully automated synchronization; developers no longer need to manually write setItem.

Tailwind v4 Final Polish

With the support of the Store, we can add someglobal feedback styles. For example, in TaskHeader.vue display task progress:

Example

After modification, the entire interface gains a progress bar feature: