当前位置:网站首页>[boutique] Pinia Persistence Based on the plug-in Pinia plugin persist
[boutique] Pinia Persistence Based on the plug-in Pinia plugin persist
2022-07-06 23:52:00 【Liang Yunliang】
preparation
First step : install pinia
npm install pinia --save
The second step : newly build store Catalog , establish index.ts
import {
createPinia } from "pinia"
const store = createPinia()
export default store
The third step :main.ts Use in store
import {
createApp } from 'vue'
import store from '@/store'
const app = createApp(App)
app.use(store)
app.mount('#app')
Step four : newly build user.ts
import {
defineStore } from "pinia"
export const useUserStore = defineStore({
id:'user',
state() {
return {
name: '',
age: 0
}
}
})
Step five : stay vue Page usage
<template>
User:{
{ name }} == {
{ age }}
<br>
<button @click="fun"> Modified value </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>
result
Open the page , Click the page button , The displayed user information changes , Refresh the page and return to its original appearance .
Using plug-ins pinia-plugin-persist Persistence
First step : Installing a plug-in pinia-plugin-persist
npm install pinia-plugin-persist --save
The second step : modify store Under folder index.ts
import {
createPinia } from "pinia"
import piniaPluginPersist from 'pinia-plugin-persist'
const store = createPinia()
store.use(piniaPluginPersist)
export default store
The third step : modify user.ts, To add 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
}
]
}
})
notes : stay strategies You can specify which data to cache , such as :
strategies: [
{
storage: localStorage, paths: ['token','name'] }
]
test
Run the program , Open the page and click the button , Data changes , Close the page and open it again , The page still shows the changed data , here F12, Will see :
边栏推荐
- How does win11 restore the traditional right-click menu? Win11 right click to change back to traditional mode
- Local deployment Zeppelin 0.10.1
- Every year, 200 billion yuan is invested in the chip field, and "China chip" venture capital is booming
- 基于SSM框架实现的房屋租赁管理系统
- Wasserstein slim gain with gradient poverty (wsgain-gp) introduction and code implementation -- missing data filling based on generated countermeasure network
- Gradle知识概括
- 吴恩达2022机器学习课程评测来了!
- Wind chime card issuing network source code latest version - commercially available
- How can Oracle CDC deserialize with jsondebeziumdeserializationschema
- 《数字经济全景白皮书》保险数字化篇 重磅发布
猜你喜欢
[OFDM communication] OFDM system signal detection based on deep learning with matlab code
Please help xampp to do sqlilab is a black
Today's sleep quality record 78 points
If the request URL contains jsessionid, the solution
Why is bat still addicted to 996 when the four-day working system is being tried out in Britain?
app通用功能测试用例
STM32 enters and wakes up the stop mode through the serial port
【OFDM通信】基于深度学习的OFDM系统信号检测附matlab代码
浅谈现在的弊端与未来的发展
基于SSM框架实现的房屋租赁管理系统
随机推荐
DAY SIX
CRMEB 商城系统如何助力营销?
Win11怎么恢复传统右键菜单?Win11右键改回传统模式的方法
Cas d'essai fonctionnel universel de l'application
The programmer said, "I'm 36 years old, and I don't want to be rolled, let alone cut."
Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
The same job has two sources, and the same link has different database accounts. Why is the database list found in the second link the first account
PDF文档签名指南
Server SMP, NUMA, MPP system learning notes.
Gradle knowledge generalization
快讯 l Huobi Ventures与Genesis公链深入接洽中
pinia 模块划分
Where does this "judge the operation type according to the op value and assemble SQL by yourself" mean? It means simply using Flink tab
MATLIB从excel表中读取数据并画出函数图像
The largest single investment in the history of Dachen was IPO today
传统企业要为 Web3 和去中心化做的 11 个准备
基于jsp+servlet+mysql框架的旅游管理系统【源码+数据库+报告】
PostgreSQL高可用之repmgr(1主2从+1witness)+Pgpool-II实现主从切换+读写分离
How to find out if the U disk file of the computer reinstallation system is hidden
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键