当前位置:网站首页>【精品】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,会看到:
边栏推荐
- How can Oracle CDC deserialize with jsondebeziumdeserializationschema
- Gradle知识概括
- Today, I met a senior test developer from Tencent and saw the ceiling of the foundation
- Common modification commands of Oracle for tables
- Microsoft win11 is still "unsatisfactory". Multi user feedback will cause frequent MSI crashes
- 不要再说微服务可以解决一切问题了
- The programmer refused the offer because of low salary, HR became angry and netizens exploded
- After 3 years of testing bytecan software, I was ruthlessly dismissed in February, trying to wake up my brother who was paddling
- Win11怎么恢复传统右键菜单?Win11右键改回传统模式的方法
- flinksql select id ,count(*) from a group by id .
猜你喜欢

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

Today's sleep quality record 78 points

英国都在试行4天工作制了,为什么BAT还对996上瘾?

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

亚朵三顾 IPO

The method of reinstalling win10 system is as simple as that

Basic chart interpretation of "Oriental selection" hot out of circle data

Cloud native (32) | kubernetes introduction to platform storage system

【OFDM通信】基于深度学习的OFDM系统信号检测附matlab代码

With the help of this treasure artifact, I became the whole stack
随机推荐
The largest single investment in the history of Dachen was IPO today
Basic chart interpretation of "Oriental selection" hot out of circle data
One minute to learn how to install the system, win7 XP, win10 and win11 become very simple
AI表现越差,获得奖金越高?纽约大学博士拿出百万重金,悬赏让大模型表现差劲的任务...
同一个作业有两个source,同一链接不同数据库账号,为何第二个链接查出来的数据库列表是第一个账号的
STM32 enters and wakes up the stop mode through the serial port
app通用功能测试用例
【OFDM通信】基于深度学习的OFDM系统信号检测附matlab代码
CRMEB 商城系统如何助力营销?
电脑重装系统u盘文件被隐藏要怎么找出来
MATLIB reads data from excel table and draws function image
The problem of ASP reading Oracle Database
App general function test cases
Ajout, suppression et modification d'un tableau json par JS
Knowledge * review
Computer reinstallation system teaching, one click fool operation, 80% of people have learned
本地部署 zeppelin 0.10.1
MySQL implementation of field segmentation from one line to multiple lines of example code
The important data in the computer was accidentally deleted by mistake, which can be quickly retrieved by this method
How to find out if the U disk file of the computer reinstallation system is hidden