Installations
npm install pinia-plugin-unistorage
Developer Guide
Typescript
Yes
Module System
ESM
Node Version
20.12.2
NPM Version
10.5.0
Score
71.1
Supply Chain
98.3
Quality
79.6
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (48.11%)
TypeScript (20.63%)
SCSS (20.09%)
HTML (6.42%)
Vue (4.74%)
Developer
dishait
Download Statistics
Total Downloads
7,838
Last Day
3
Last Week
44
Last Month
326
Last Year
4,026
GitHub Statistics
18 Stars
55 Commits
2 Forks
1 Watching
1 Branches
2 Contributors
Bundle Size
2.36 kB
Minified
1.04 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.1.2
Package Id
pinia-plugin-unistorage@0.1.2
Unpacked Size
14.31 kB
Size
5.05 kB
File Count
5
NPM Version
10.5.0
Node Version
20.12.2
Publised On
17 Jul 2024
Total Downloads
Cumulative downloads
Total Downloads
7,838
Last day
-84.2%
3
Compared to previous day
Last week
-60%
44
Compared to previous week
Last month
31.5%
326
Compared to previous month
Last year
47.4%
4,026
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
4
pinia-plugin-unistorage
uniapp 下 pinia 的本地数据缓存插件
引用
该插件是
pinia-plugin-persistedstate
的 uniapp
版本,如果你需要在纯 vue
或者 nuxt
项目中使用 pinia
的本地数据缓存,请使用
pinia-plugin-persistedstate。
动机
为了实现多端的更简单的全局本地数据缓存
组织 🦔
欢迎关注 帝莎编程
使用
安装
1. cli
创建的 uniapp
项目
1npm i pinia-plugin-unistorage -D
1// main.js 2import { createSSRApp } from "vue"; 3import * as Pinia from "pinia"; 4import { createUnistorage } from "pinia-plugin-unistorage"; 5 6export function createApp() { 7 const app = createSSRApp(App); 8 9 const store = Pinia.createPinia(); 10 11 // 关键代码 👇 12 store.use(createUnistorage()); 13 14 app.use(store); 15 16 return { 17 app, 18 Pinia, // 此处必须将 Pinia 返回 19 }; 20}
2. hbuilderx
创建的 uniapp
项目
👉 直接插件市场安装 后引入注册
1// main.js 2import { createSSRApp } from "vue"; 3import * as Pinia from "pinia"; 4import { createUnistorage } from "./uni_modules/pinia-plugin-unistorage"; 5 6export function createApp() { 7 const app = createSSRApp(App); 8 9 const store = Pinia.createPinia(); 10 11 // 关键代码 👇 12 store.use(createUnistorage()); 13 14 app.use(store); 15 16 return { 17 app, 18 Pinia, // 此处必须将 Pinia 返回 19 }; 20}
基础
1import { defineStore } from "pinia"; 2 3export const useStore = defineStore("main", { 4 state() { 5 return { 6 someState: "hello pinia", 7 }; 8 }, 9 unistorage: true, // 开启后对 state 的数据读写都将持久化 10});
或者 setup
语法也是支持的
1import { defineStore } from "pinia"; 2 3export const useStore = defineStore( 4 "main", 5 () => { 6 const someState = ref("hello pinia"); 7 return { someState }; 8 }, 9 { 10 unistorage: true, // 开启后对 state 的数据读写都将持久化 11 }, 12);
选项
钩子
1import { defineStore } from "pinia"; 2 3export const useStore = defineStore("main", { 4 state() { 5 return { 6 someState: "hello pinia", 7 }; 8 }, 9 unistorage: { 10 // 初始化恢复前触发 11 beforeRestore(ctx) {}, 12 // 初始化恢复后触发 13 afterRestore(ctx) {}, 14 }, 15});
序列化
大多数情况下你并不需要了解该选项
1import { defineStore } from "pinia"; 2 3export const useStore = defineStore("main", { 4 state() { 5 return { 6 someState: "hello pinia", 7 }; 8 }, 9 unistorage: { 10 serializer: { 11 // 序列化,默认为 JSON.stringify 12 serialize(v) { 13 return JSON.stringify(v); 14 }, 15 // 反序列化,默认为 JSON.parse 16 deserialize(v) { 17 return JSON.parse(v); 18 }, 19 }, 20 }, 21});
其他
1import { defineStore } from "pinia"; 2 3export const useStore = defineStore("main", { 4 state() { 5 return { 6 foo: "foo", 7 nested: { 8 data: "nested pinia", 9 }, 10 someState: "hello pinia", 11 }; 12 }, 13 unistorage: { 14 key: "foo", // 缓存的键,默认为该 store 的 id,这里是 main, 15 paths: ["foo", "nested.data"], // 需要缓存的路径,这里设置 foo 和 nested 下的 data 会被缓存 16 }, 17});
License
Made with markthree
Published under MIT License.
No vulnerabilities found.
No security vulnerabilities found.