返回介绍

Interface: DefineStoreOptions

发布于 2024-04-14 00:17:41 字数 5838 浏览 0 评论 0 收藏 0

pinia.DefineStoreOptions

Options parameter of defineStore() for option stores. Can be extended to augment stores with the plugin API.

See

DefineStoreOptionsBase.

Type parameters

NameType
Idextends string
Sextends StateTree
GG
AA

Hierarchy

Properties

actions

Optional actions: A & ThisType<A & UnwrapRef<S> & _StoreWithState<Id, S, G, A> & _StoreWithGetters<G> & PiniaCustomProperties<string, StateTree, _GettersTree<StateTree>, _ActionsTree>>

Optional object of actions.


getters

Optional getters: G & ThisType<UnwrapRef<S> & _StoreWithGetters<G> & PiniaCustomProperties<string, StateTree, _GettersTree<StateTree>, _ActionsTree>> & _GettersTree<S>

Optional object of getters.


id

id: Id

Unique string key to identify the store across the application.


state

Optional state: () => S

Type declaration

▸ (): S

Function to create a fresh state. Must be an arrow function to ensure correct typings!

Returns

S

Methods

hydrate

Optional hydrate(storeState, initialState): void

Allows hydrating the store during SSR when complex state (like client side only refs) are used in the store definition and copying the value from pinia.state isn't enough.

Example

If in your state, you use any customRefs, any computeds, or any refs that have a different value on Server and Client, you need to manually hydrate them. e.g., a custom ref that is stored in the local storage:

ts
const useStore = defineStore('main', {
  state: () => ({
    n: useLocalStorage('key', 0)
  }),
  hydrate(storeState, initialState) {
    // @ts-expect-error: https://github.com/microsoft/TypeScript/issues/43826
    storeState.n = useLocalStorage('key', 0)
  }
})

Parameters

NameTypeDescription
storeStateUnwrapRef<S>the current state in the store
initialStateUnwrapRef<S>initialState

Returns

void

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文