Skip to content

Commit

Permalink
docs(vue): 新增pinia应用示例
Browse files Browse the repository at this point in the history
  • Loading branch information
simply-none committed Sep 8, 2024
1 parent 8a3fd8a commit 9329edd
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion docs/usage-frame/vue/Pinia.md
Original file line number Diff line number Diff line change
Expand Up @@ -852,4 +852,25 @@ export const useAuthStore = defineStore('auth', {
user: useLocalStorage('pinia/auth/login', 'bob')
})
})
```
```

## 应用

### 在组件外使用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)
}

```

0 comments on commit 9329edd

Please sign in to comment.