From: Eduardo San Martin Morote Date: Tue, 23 Mar 2021 09:07:55 +0000 (+0100) Subject: docs: getters and actions X-Git-Tag: v2.0.0-alpha.8~20 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=14e36f5288442c997d4a3c8937e2e95a034109e1;p=thirdparty%2Fvuejs%2Fpinia.git docs: getters and actions --- diff --git a/docs/core-concepts/actions.md b/docs/core-concepts/actions.md index 651a4ee9..319accc0 100644 --- a/docs/core-concepts/actions.md +++ b/docs/core-concepts/actions.md @@ -9,8 +9,8 @@ export const useStore = defineStore({ counter: 0, }), actions: { - reset() { - this.counter = 0 + randomizeCounter() { + this.counter = Math.round(100 * Math.random()) }, }, }) @@ -25,9 +25,34 @@ export default defineComponent({ setup() { const main = useMainStore() // call the action as a method of the store - main.reset() + main.randomizeCounter() return {} }, }) ``` + +## Accessing other stores actions + +To use another store, you can directly _use it_ inside of the _getter_: + +```js +import { useOtherStore } from './other-store' + +export const useSettingsStore = defineStore({ + id: 'settings', + state: () => ({ + // ... + }), + actions: { + async fetchUserPreferences(preferences) { + const auth = useAuthStore() + if (auth.isAuthenticated) { + this.preferences = await fetchPreferences() + } else { + throw new Error('User must be authenticated') + } + }, + }, +}) +``` diff --git a/docs/core-concepts/getters.md b/docs/core-concepts/getters.md index e11cef2e..1b837fd6 100644 --- a/docs/core-concepts/getters.md +++ b/docs/core-concepts/getters.md @@ -52,15 +52,15 @@ export const useStore = defineStore({ }, doubleCountPlusOne() { // autocompletion ✨ - return this.doubleCount * 2 + return this.doubleCount + 1 }, }, }) ``` -## Accessing other stores +## Accessing other stores getters -To access a different store, you can directly _use_ the other store inside of a `getter` +To use another store getters, you can directly _use it_ inside of the _action_: ```js import { useOtherStore } from './other-store' diff --git a/docs/core-concepts/state.md b/docs/core-concepts/state.md index af650421..858ddfa8 100644 --- a/docs/core-concepts/state.md +++ b/docs/core-concepts/state.md @@ -42,7 +42,7 @@ store.$patch({ -The main difference here is that `$patch()` allows you to group multiple changes into one single entry in the devtools. Note **both, direct changes to `state` and `$patch()` appear in the devtools**. +The main difference here is that `$patch()` allows you to group multiple changes into one single entry in the devtools. Note **both, direct changes to `state` and `$patch()` appear in the devtools** and can be time travelled (not yet in Vue 3). ## Replacing the `state`