当前位置:网站首页>【精品】pinia 基于插件pinia-plugin-persist的 持久化
【精品】pinia 基于插件pinia-plugin-persist的 持久化
2022-07-06 16:25:00 【梁云亮】
准备工作
第一步:安装pinia
npm install pinia --save
第二步:新建store目录,创建index.ts
import {
createPinia } from "pinia"
const store = createPinia()
export default store
第三步:main.ts中使用store
import {
createApp } from 'vue'
import store from '@/store'
const app = createApp(App)
app.use(store)
app.mount('#app')
第四步:新建user.ts
import {
defineStore } from "pinia"
export const useUserStore = defineStore({
id:'user',
state() {
return {
name: '',
age: 0
}
}
})
第五步:在vue页面中使用
<template>
User:{
{ name }} == {
{ age }}
<br>
<button @click="fun">修改值</button>
</template>
<script setup lang="ts"> import {
useUserStore} from "@/store/module/user" import {
storeToRefs} from "pinia" const userStore = useUserStore() const {
name, age} = storeToRefs(userStore) const fun = () => {
userStore.name = "zhangsan" userStore.age = 18 } </script>
结果
打开页面,单击页面按钮,显示的用户信息发生变化,刷新页面又恢复到原来的样子。
利用插件pinia-plugin-persist持久化
第一步:安装插件pinia-plugin-persist
npm install pinia-plugin-persist --save
第二步:修改store文件夹下的index.ts
import {
createPinia } from "pinia"
import piniaPluginPersist from 'pinia-plugin-persist'
const store = createPinia()
store.use(piniaPluginPersist)
export default store
第三步:修改user.ts,为其添加persist
import {
defineStore} from "pinia"
export const useUserStore = defineStore({
id: "user",
state: () => {
return {
name: '',
age: 0
}
},
persist: {
enabled: true,
strategies: [
{
key: 'wego_user',
storage: localStorage
}
]
}
})
注:在strategies中可以指定对哪些数据进行缓存,比如:
strategies: [
{
storage: localStorage, paths: ['token','name'] }
]
测试
运行程序,打开页面单击按钮,数据发生变化,关闭页面再次打开,页面显示的仍然是变化后的数据,此时F12,会看到:
边栏推荐
- 传统企业要为 Web3 和去中心化做的 11 个准备
- The programmer refused the offer because of low salary, HR became angry and netizens exploded
- MATLIB从excel表中读取数据并画出函数图像
- Scholar doctor hahaha
- How to implement Lua entry of API gateway
- 若依请求url中带有jsessionid的解决办法
- 基于jsp+servlet+mysql框架的旅游管理系统【源码+数据库+报告】
- 快手的新生意,还得靠辛巴吆喝?
- okcc呼叫中心的订单管理时怎么样的
- The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
猜你喜欢
With the help of this treasure artifact, I became the whole stack
英国都在试行4天工作制了,为什么BAT还对996上瘾?
The problem of ASP reading Oracle Database
The intranet penetrates the zerotier extranet (mobile phone, computer, etc.) to access intranet devices (raspberry pie, NAS, computer, etc.)
Why is bat still addicted to 996 when the four-day working system is being tried out in Britain?
Entropy information entropy cross entropy
达晨史上最大单笔投资,今天IPO了
App general function test cases
[unmanned aerial vehicle] multi unmanned cooperative task allocation program platform, including Matlab code
电脑重装系统u盘文件被隐藏要怎么找出来
随机推荐
[communication] optimal power allocation in the uplink of two-layer wireless femtocell network with matlab code
Who said that new consumer brands collapsed? Someone behind me won
The intranet penetrates the zerotier extranet (mobile phone, computer, etc.) to access intranet devices (raspberry pie, NAS, computer, etc.)
Leetcode problem solving - 889 Construct binary tree according to preorder and postorder traversal
本地部署 zeppelin 0.10.1
CRMEB 商城系统如何助力营销?
基于jsp+servlet+mysql框架的旅游管理系统【源码+数据库+报告】
The programmer refused the offer because of low salary, HR became angry and netizens exploded
【212】php发送post请求有哪三种方法
After 3 years of testing bytecan software, I was ruthlessly dismissed in February, trying to wake up my brother who was paddling
Penetration test --- database security: detailed explanation of SQL injection into database principle
leetcode:236. The nearest common ancestor of binary tree
Design a red envelope grabbing system
Should the jar package of MySQL CDC be placed in different places in the Flink running mode?
The programmer said, "I'm 36 years old, and I don't want to be rolled, let alone cut."
这个『根据 op 值判断操作类型来自己组装 sql』是指在哪里实现?是指单纯用 Flink Tabl
公链与私链在数据隐私和吞吐量上的竞争
The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
MySQL connected vscode successfully, but this error is reported
Computer reinstallation system teaching, one click fool operation, 80% of people have learned