From: Eduardo San Martin Morote Date: Sun, 24 Jul 2022 19:27:43 +0000 (+0200) Subject: docs: nested stores example X-Git-Tag: @pinia/nuxt@0.3.1~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0cb2c9821d2a530384bed82ca8418ac271f510e2;p=thirdparty%2Fvuejs%2Fpinia.git docs: nested stores example --- diff --git a/packages/docs/cookbook/composing-stores.md b/packages/docs/cookbook/composing-stores.md index 8de80560..2100c5cc 100644 --- a/packages/docs/cookbook/composing-stores.md +++ b/packages/docs/cookbook/composing-stores.md @@ -44,6 +44,26 @@ const useY = defineStore('y', () => { Note that if one store uses another store, you can directly import and call the `useStore()` function within _actions_ and _getters_. Then you can interact with the store just like you would from within a Vue component. See [Shared Getters](#shared-getters) and [Shared Actions](#shared-actions). +When it comes to _setup stores_, you can simply use one of the stores **at the top** of the store function: + +```ts +import { useUserStore } from './user' + +export const useCartStore = defineStore('cart', () => { + const user = useUserStore() + + const summary = computed(() => { + return `Hi ${user.name}, you have ${state.list.length} items in your cart. It costs ${state.price}.` + }) + + function purchase() { + return apiPurchase(user.id, this.list) + } + + return { summary, purchase } +}) +``` + ## Shared Getters You can simply call `useOtherStore()` inside a _getter_: