当前位置:网站首页>[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 :
边栏推荐
- MATLIB reads data from excel table and draws function image
- Wasserstein Slim GAIN with Gradient Penalty(WSGAIN-GP)介绍及代码实现——基于生成对抗网络的缺失数据填补
- 每日刷题记录 (十五)
- 【212】php发送post请求有哪三种方法
- Let me ask you if there are any documents or cases of flynk SQL generation jobs. I know that flynk cli can create tables and specify items
- The intranet penetrates the zerotier extranet (mobile phone, computer, etc.) to access intranet devices (raspberry pie, NAS, computer, etc.)
- Knowledge * review
- Computer reinstallation system teaching, one click fool operation, 80% of people have learned
- Leetcode problem solving - 889 Construct binary tree according to preorder and postorder traversal
- After 3 years of testing bytecan software, I was ruthlessly dismissed in February, trying to wake up my brother who was paddling
猜你喜欢

自动化测试工具Katalon(Web)测试操作说明

每日刷题记录 (十五)

Résumé des connaissances de gradle

Zero code and high return. How to use 40 sets of templates to meet 95% of the reporting needs in the work

快讯 l Huobi Ventures与Genesis公链深入接洽中

AI金榜题名时,MLPerf榜单的份量究竟有多重?

《数字经济全景白皮书》保险数字化篇 重磅发布

App general function test cases
![[unmanned aerial vehicle] multi unmanned cooperative task allocation program platform, including Matlab code](/img/4c/5d867437aac5faa299817e187602e1.png)
[unmanned aerial vehicle] multi unmanned cooperative task allocation program platform, including Matlab code

零代码高回报,如何用40套模板,能满足工作中95%的报表需求
随机推荐
每日刷题记录 (十五)
DAY ONE
okcc呼叫中心的订单管理时怎么样的
MySQL implementation of field segmentation from one line to multiple lines of example code
【自动化测试框架】关于unittest你需要知道的事
PostgreSQL高可用之repmgr(1主2从+1witness)+Pgpool-II实现主从切换+读写分离
基于SSM框架实现的房屋租赁管理系统
Win11怎么恢复传统右键菜单?Win11右键改回传统模式的方法
Implementation steps of mysql start log in docker
How much does the mlperf list weigh when AI is named?
Gold three silver four, don't change jobs
Résumé des connaissances de gradle
Newsletter L Huobi ventures is in-depth contact with genesis public chain
JDBC programming of MySQL database
STM32通过串口进入和唤醒停止模式
[212] what are three methods for PHP to send post requests
传统企业要为 Web3 和去中心化做的 11 个准备
PDF文档签名指南
Zero code and high return. How to use 40 sets of templates to meet 95% of the reporting needs in the work
为什么完全背包要用顺序遍历?简要解释一下