Skip to content

Latest commit

 

History

History
95 lines (69 loc) · 1.56 KB

README.zh-CN.md

File metadata and controls

95 lines (69 loc) · 1.56 KB

pinia-persistedstate-plugin

中文 | English

一个为 vue3 准备的和 vuex-persistedstate 类似的 pinia 插件

安装

npm install pinia-persistedstate-plugin

或者

yarn add pinia-persistedstate-plugin

或者

pnpm add pinia-persistedstate-plugin

UMD

<script src="https://unpkg.com/pinia-persistedstate-plugin/dist/pinia-persistedstate-plugin.umd.js"></script>

插件为 window 添加全局变量 PiniaPersistedstatePlugin

使用

main.ts:

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import { createPersistedState } from 'pinia-persistedstate-plugin'
import App from './App.vue'

const app = createApp(App)
const store = createPinia()

store.use(createPersistedState())

app.use(store).mount('#app')

使用 js-cookie

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import { createPersistedState } from 'pinia-persistedstate-plugin'
import App from './App.vue'
import Cookies from 'js-cookie'

const app = createApp(App)
const store = createPinia()

store.use(
  createPersistedState({
    storage: Cookies
  })
)

app.use(store).mount('#app')

选项

export interface Options {
  /**
   * 存储类型,默认为 `window.localStorage`
   *
   * 支持 `js-cookie` `window.localStorage` `window.sessionStorage`
   */
  storage?: Storage | Cookies
  /**
   * 存储的key值前缀,默认为 `pinia`
   */
  key: string
  /**
   * 是否开启日志,默认为 `false`
   */
  logger: boolean
}

授权

MIT 授权。