vue3中数据的pinia的使用

一、安装pinia

npm i pinia

二、全局注册

import { createApp } from 'vue'

import { createPinia } from 'pinia'

import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'

import './style.css'

import App from './App.vue'

const pinia = createPinia()

pinia.use(piniaPluginPersistedstate)

const app = createApp(App)

app.use(pinia)

app.mount('#app')

三、定义仓库

import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', {

// 定义状态

state: () => ({

count: 0,

name: 'Eduardo',

field1: 'some data',

field2: 'other data'

}),

// 持久化配置

persist: {

key: 'counter-data',

storage: localStorage,

},

// 计算属性

getters: {

doubleCount: (state) => state.count * 2,

doubleCountPlus: (state) => state.count * 2 + 10

},

// 方法

actions: {

increment() {

this.count++

},

decrement() {

this.count--

},

incrementByAmount(amount) {

this.count += amount

},

// 添加一个重置方法用于测试

reset() {

this.count = 0

this.name = 'Eduardo'

this.field1 = 'some data'

this.field2 = 'other data'

}

},

})

四、使用

<template>

<div style="padding: 20px; border: 1px solid blue;">

<h2>计数器测试 (CounterTest)</h2>

<p>计数值: {{ counterStore.count }}</p>

<p>双倍计数: {{ counterStore.doubleCount }}</p>

<p>名称: {{ counterStore.name }}</p>

<div style="margin: 10px 0;">

<button @click="counterStore.increment()">+1</button>

<button @click="counterStore.decrement()">-1</button>

<button @click="counterStore.incrementByAmount(5)">+5</button>

<button @click="updateName">更新名称</button>

</div>

<div>

<input v-model="counterStore.count" type="number" placeholder="直接编辑计数" />

</div>

<div style="margin-top: 20px;">

<h3>本地存储检查</h3>

<p>localStorage中的值: {{ localStorageValue }}</p>

<p>Store的持久化配置: {{ persistConfig }}</p>

<button @click="refreshPage">刷新页面测试持久化</button>

</div>

</div>

</template>

<script setup>

import { useCounterStore } from '../stores/counter.js'

import { ref, onMounted, computed } from 'vue'

const counterStore = useCounterStore()

const localStorageValue = ref('')

const persistConfig = ref('')

const updateName = () => {

counterStore.name = `访客${Math.floor(Math.random() * 100)}`

}

const refreshPage = () => {

window.location.reload()

}

onMounted(() => {

// 读取localStorage中的值以检查是否正确保存

const storedValue = localStorage.getItem('counter-data')

localStorageValue.value = storedValue ? JSON.parse(storedValue).state : '未找到'

// 检查store的持久化配置

persistConfig.value = counterStore.$persist ? '已配置' : '未配置'

// 打印整个store状态

console.log('Store State:', counterStore.$state)

console.log('Persist Info:', counterStore.$persist)

})

</script>

相关推荐
奇特認1 小时前
mysql 集群技术 3.高可用之 MHA
数据库·mysql
油丶酸萝卜别吃1 小时前
utils.js 说明文档
开发语言·javascript·ecmascript
zyplayer-doc3 小时前
VuePress类静态文档站和动态知识库怎么选:两种技术路线的适用场景
javascript·人工智能·后端·安全·智能手机
Bioinfo Guy4 小时前
高分Panel复现系列|转录因子网络太乱?用模块框把重点圈出来
数据库·生物信息学·生信可视化
紫禁玄科5 小时前
Shai-Hulud:npm生态的自我复制蠕虫风暴
前端·npm·node.js
ltl5 小时前
Disaggregated DB 合集:Socrates、PolarDB、Taurus 的共同模式
数据库
东风破_5 小时前
后端API没写好,前端难道干等着吗?
前端
用户938515635075 小时前
从前后端分离到前端接口工程:React + MockJS + Vite 解析
前端·后端·全栈
用户938515635075 小时前
从零在浏览器里跑 DeepSeek-R1:WebGPU + Transformers.js 全链路实战(三)
前端·react.js·typescript
excel6 小时前
当前端行情变差,我们为什么还要坚持?
前端