web前端全系列 教程
1839个小节阅读:2244.1k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
pinia
有个副作用,就是无法持久化,在浏览器刷新重置之后,会全部回复默认,这时候,我们可以利用插件实现本地持久化存储
xxxxxxxxxx
// 仓库
import { defineStore } from "pinia"
export const useCountStore = defineStore("count",{
state:() =>{
return{
count:10
}
}
})
xxxxxxxxxx
// 引入仓库
import { createApp } from 'vue'
import App from './App.vue'
import { createPinia } from "pinia"
import { piniaStoragePlugins } from "./stores/plugins"
const pinia = createPinia()
pinia.use(piniaStoragePlugins)
createApp(App).use(pinia).mount('#app')
xxxxxxxxxx
// 本地存储
export function setStorage(key,value){
localStorage.setItem(key,value)
}
export function getStorage(key){
return localStorage.getItem(key)
}
xxxxxxxxxx
// 实现插件,本地存储
import { setStorage, getStorage } from "../../utils/storage"
export function piniaStoragePlugins({ store }) {
if (getStorage("count")) {
store.count = getStorage("count")
}else{
setStorage("count", store.count)
}
store.$subscribe(() => {
setStorage("count", store.count)
})
}
xxxxxxxxxx
// 视图
<template>
<h3>Pinia插件</h3>
<p>{{ store.count }}</p>
<button @click="addHandler">增加</button>
</template>
<script setup>
import { useCountStore } from "../stores/count"
const store = useCountStore()
function addHandler(){
store.count++
}
</script>