From 9329edd982339ba7fcdeb26a8a5a2f2569b8d97d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AE=80=E9=9A=90?= <44159358+simply-none@users.noreply.github.com> Date: Sun, 8 Sep 2024 13:53:50 +0800 Subject: [PATCH] =?UTF-8?q?docs(vue):=20=E6=96=B0=E5=A2=9Epinia=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/usage-frame/vue/Pinia.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/usage-frame/vue/Pinia.md b/docs/usage-frame/vue/Pinia.md index 101c489..c49f050 100644 --- a/docs/usage-frame/vue/Pinia.md +++ b/docs/usage-frame/vue/Pinia.md @@ -852,4 +852,25 @@ export const useAuthStore = defineStore('auth', { user: useLocalStorage('pinia/auth/login', 'bob') }) }) -``` \ No newline at end of file +``` + +## 应用 + +### 在组件外使用pinia + +为了兼容在组件外使用pinia的情形,需要将pinia store实例用函数包裹起来,用法如下: + +```typescript +import { createPinia, defineStore } from 'pinia' + +const pinia = createPinia() + +// 只能在调用createPinia后使用 +export const useUserStore = defineStore('user', {}) + +// 这种方式使用,可以兼容到组件外使用的情况 +export function useUserStoreHook () { + return useUserStore(pinia) +} + +```