当前位置:网站首页>[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 :
边栏推荐
- Gradle知識概括
- [system analyst's road] Chapter 7 double disk system design (service-oriented development method)
- js导入excel&导出excel
- 在Docker中分分钟拥有Oracle EMCC 13.5环境
- 公链与私链在数据隐私和吞吐量上的竞争
- I've been laid off, and I'll lose money for everything. The days when I once made a monthly salary of 20000 are not coming back
- Oracle中使用包FY_Recover_Data.pck来恢复truncate误操作的表
- MySQL implementation of field segmentation from one line to multiple lines of example code
- app通用功能测试用例
- Can async i/o be implemented by UDF operator and then called by SQL API? At present, it seems that only datastre can be seen
猜你喜欢
MVC and MVVM
每日刷题记录 (十五)
The important data in the computer was accidentally deleted by mistake, which can be quickly retrieved by this method
DAY TWO
自动化测试工具Katalon(Web)测试操作说明
JDBC programming of MySQL database
Entropy information entropy cross entropy
MATLIB从excel表中读取数据并画出函数图像
基础图表解读“东方甄选”爆火出圈数据
I've been laid off, and I'll lose money for everything. The days when I once made a monthly salary of 20000 are not coming back
随机推荐
Experiment 6: installing eve-ng
okcc呼叫中心的订单管理时怎么样的
How to find out if the U disk file of the computer reinstallation system is hidden
基于SSM框架实现的房屋租赁管理系统
1000字精选 —— 接口测试基础
Wasserstein slim gain with gradient poverty (wsgain-gp) introduction and code implementation -- missing data filling based on generated countermeasure network
MATLIB从excel表中读取数据并画出函数图像
Automatic test tool katalon (WEB) test operation instructions
编译logisim
js导入excel&导出excel
Talking about the current malpractice and future development
openresty ngx_lua子请求
How rider uses nuget package offline
SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
Please help xampp to do sqlilab is a black
Eureka Client启动后就关闭 Unregistering application xxx with eureka with status DOWN
Oracle中使用包FY_Recover_Data.pck来恢复truncate误操作的表
There are only two TXT cells in the ArrayExpress database. Can you only download the sequencing run matrix from line to ENA?
How to implement Lua entry of API gateway
氢创未来 产业加速 | 2022氢能专精特新创业大赛报名通道开启!