# pinia-plugin-persistence-mp

> pinia持久化存储插件，适用于uni-app、taro

Latest version **0.0.1** (published 2022-10-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install pinia-plugin-persistence-mp
pnpm add pinia-plugin-persistence-mp
yarn add pinia-plugin-persistence-mp
bun add pinia-plugin-persistence-mp
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2022-10-26 |
| First published | 2022-10-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | wnor543 |
| Maintainers | wnor543 |
| Keywords | pinia, persistence, uni-app, taro, vue |

## Links

- npm: https://www.npmjs.com/package/pinia-plugin-persistence-mp
- Repository: https://github.com/side-projects-list/pinia-plugin-persistence-mp
- Issues: https://github.com/side-projects-list/pinia-plugin-persistence-mp/issues
- npm.io page: https://npm.io/package/pinia-plugin-persistence-mp

## Alternatives

- [lodash.startswith](https://npm.io/package/lodash.startswith.md) — 769.7K weekly downloads
- [@tarojs/service](https://npm.io/package/@tarojs/service.md) — 33.9K weekly downloads
- [io.extendreality.tilia.indicators.spatialtargets.unity](https://npm.io/package/io.extendreality.tilia.indicators.spatialtargets.unity.md) — 131 weekly downloads
- [@rtarojs/taro](https://npm.io/package/@rtarojs/taro.md) — 90 weekly downloads
- [node-branch-io](https://npm.io/package/node-branch-io.md) — 50 weekly downloads

## Recent versions

- 0.0.1 (latest) — 2022-10-26

## README

# pinia-plugin-persistence-mp

> 基于[pinia-plugin-persistedstate](https://github.com/prazdevs/pinia-plugin-persistedstate)修改而来

`pinia-plugin-persistence-mp`是 **pinia** 持久化存储插件，适用于[uni-app](https://uniapp.dcloud.net.cn/)、[taro](https://taro.jd.com/)。

## 🚀 快速开始

1. 选择您喜欢的包管理器安装:
   - **pnpm** : `pnpm i pinia-plugin-persistence-mp`
   - npm : `npm i pinia-plugin-persistence-mp`
   - yarn : `yarn add pinia-plugin-persistence-mp`

2. 添加插件到 **pinia** 中:
   ```ts
   // main.ts
   import { createPinia } from 'pinia'
   import { createPersistedState } from 'pinia-plugin-persistence-mp'

   const pinia = createPinia()
   pinia.use(createPersistedState(
     {
       // 全局配置存储操作对象，根据使用的是 uni-app 或 taro 而定
       storage: {
         // uni-app 方式
         setStorageSync: uni.setStorageSync,
         getStorageSync: uni.getStorageSync
       }
     },
   ))

   // taro 方式
   // import {getStorageSync, setStorageSync} from '@tarojs/taro'
   // pinia.use(createPersistedState(
   //    {
   //        // 全局配置存储操作对象
   //        storage: {
   //            setStorageSync,
   //            getStorageSync
   //        }
   //    },
   // ))
   ```

3. 添加 `persist` 选项到您想要持久化存储的 **store** 中:
    ```ts
    // store/counter.ts
    import { defineStore } from 'pinia'
    import { ref } from 'vue'

    export const useCounterStore = defineStore(
      'counter',
      () => {
        const count = ref(0)
        return {
          count
        }
      },
      {
        persist: true
      }
    )
    ```

## 🔧 配置
您可以通过指定持久化属性的选项来配置 **store** 的持久化方式。

```ts
// store/counter.ts
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useCounterStore = defineStore(
  'counter',
  () => {
    const count = ref(0)
    return {
      count
    }
  },
  {
    persist: {
      storage: {
        // 单个store设置存储操作对象
        setStorageSync: uni.setStorageSync,
        getStorageSync: uni.getStorageSync
      },
      // 指定需要持久化的state
      paths: ['count']
    }
  }
)
```

更多的配置选项说明：[传送门](https://prazdevs.github.io/pinia-plugin-persistedstate/guide/config.html)

---
> ⚠️注意：必须要在全局或单个 store 里设置`persist.storage`为具体的存储操作对象，否则找不到对应的 `setStorageSync` 和 `getStorageSync` 方法。


## ⚠️ 限制

[插件所存在的限制](https://prazdevs.github.io/pinia-plugin-persistedstate/guide/limitations.html)

---
_Source: https://npm.io/package/pinia-plugin-persistence-mp · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
