-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pinia persist plugin (#3173)
- Loading branch information
Showing
4 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Pinia Persist Plugin | ||
* Pinia 持久化插件 | ||
* @link https://prazdevs.github.io/pinia-plugin-persistedstate/zh/guide/ | ||
* | ||
*/ | ||
import type { Pinia } from 'pinia'; | ||
import { createPersistedState } from 'pinia-plugin-persistedstate'; | ||
import type { PersistedStateFactoryOptions } from 'pinia-plugin-persistedstate'; | ||
import { getCommonStoragePrefix } from '@/utils/env'; | ||
|
||
export const PERSIST_KEY_PREFIX = getCommonStoragePrefix(); | ||
|
||
// TODO customSerializer | ||
|
||
/** | ||
* Register Pinia Persist Plugin | ||
* 注册 Pinia 持久化插件 | ||
* | ||
* @param pinia Pinia instance Pinia 实例 | ||
*/ | ||
export function registerPiniaPersistPlugin(pinia: Pinia) { | ||
pinia.use(createPersistedState(createPersistedStateOptions(PERSIST_KEY_PREFIX))); | ||
} | ||
|
||
/** | ||
* Create Persisted State Options | ||
* 创建持久化状态选项 | ||
* | ||
* @param keyPrefix prefix for storage key 储存键前缀 | ||
* @returns persisted state factory options | ||
*/ | ||
export function createPersistedStateOptions(keyPrefix: string): PersistedStateFactoryOptions { | ||
return { | ||
storage: localStorage, | ||
key: (id) => `${keyPrefix}__${id}`, | ||
}; | ||
} |