You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please note that if you are using Vue 2, `@vue/test-utils` requires a [slightly different configuration](#Unit-test-components-Vue-2-).
113
-
114
112
### Initial State
115
113
116
114
You can set the initial state of **all of your stores** when creating a testing pinia by passing an `initialState` object. This object will be used by the testing pinia to _patch_ stores when they are created. Let's say you want to initialize the state of this store:
When it comes to Pinia, you don't need to change anything for E2E tests, that's the whole point of these tests! You could maybe test HTTP requests, but that's way beyond the scope of this guide 😄.
294
-
295
-
## Unit test components (Vue 2)
296
-
297
-
When using [Vue Test Utils 1](https://v1.test-utils.vuejs.org/), install Pinia on a `localVue`:
298
-
299
-
```js
300
-
import { PiniaVuePlugin } from'pinia'
301
-
import { createLocalVue, mount } from'@vue/test-utils'
Copy file name to clipboardExpand all lines: packages/docs/core-concepts/plugins.md
+4-50Lines changed: 4 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,7 @@ A Pinia plugin is a function that optionally returns properties to be added to a
44
44
```js
45
45
exportfunctionmyPiniaPlugin(context) {
46
46
context.pinia// the pinia created with `createPinia()`
47
-
context.app// the current app created with `createApp()` (Vue 3 only)
47
+
context.app// the current app created with `createApp()`
48
48
context.store// the store the plugin is augmenting
49
49
context.options// the options object defining the store passed to `defineStore()`
50
50
// ...
@@ -143,28 +143,6 @@ pinia.use(({ store }) => {
143
143
144
144
Note that state changes or additions that occur within a plugin (that includes calling `store.$patch()`) happen before the store is active and therefore **do not trigger any subscriptions**.
145
145
146
-
:::warning
147
-
If you are using **Vue 2**, Pinia is subject to the [same reactivity caveats](https://v2.vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats) as Vue. You will need to use `Vue.set()` (Vue 2.7) or `set()` (from `@vue/composition-api` for Vue <2.7) for when creating new state properties like `secret` and `hasError`:
148
-
149
-
```js
150
-
import { set, toRef } from'@vue/composition-api'
151
-
pinia.use(({ store }) => {
152
-
if (!store.$state.hasOwnProperty('secret')) {
153
-
constsecretRef=ref('secret')
154
-
// If the data is meant to be used during SSR, you should
155
-
// set it on the `$state` property so it is serialized and
156
-
// picked up during hydration
157
-
set(store.$state, 'secret', secretRef)
158
-
}
159
-
// set it directly on the store too so you can access it
160
-
// both ways: `store.$state.secret` / `store.secret`
By default, `$reset()` will not reset state added by plugins but you can override it to also reset the state you add:
@@ -395,10 +373,12 @@ declare module 'pinia' {
395
373
```
396
374
397
375
:::tip
376
+
398
377
There is also a `StoreGetters` type to extract the _getters_ from a Store type. You can also extend the options of _setup stores_ or _option stores_**only** by extending the types `DefineStoreOptions` and `DefineSetupStoreOptions` respectively.
378
+
399
379
:::
400
380
401
-
## Nuxt.js
381
+
## Nuxt
402
382
403
383
When [using pinia alongside Nuxt](../ssr/nuxt.md), you will have to create a [Nuxt plugin](https://nuxt.com/docs/guide/directory-structure/plugins) first. This will give you access to the `pinia` instance:
404
384
@@ -427,32 +407,6 @@ The above example is using TypeScript, you have to remove the type annotations `
427
407
428
408
:::
429
409
430
-
### Nuxt.js 2
431
-
432
-
If you are using Nuxt.js 2, the types are slightly different:
433
-
434
-
```ts{3,15-17}
435
-
// plugins/myPiniaPlugin.ts
436
-
import { PiniaPluginContext } from 'pinia'
437
-
import { Plugin } from '@nuxt/types'
438
-
439
-
function MyPiniaPlugin({ store }: PiniaPluginContext) {
If you are using Vue 2, the data you create in `state` follows the same rules as the `data` in a Vue instance, i.e. the state object must be plain and you need to call `Vue.set()` when **adding new** properties to it. **See also: [Vue#data](https://v2.vuejs.org/v2/api/#data)**.
34
+
35
+
In order for Vue to properly detect state, you must declare every state piece in `data`, even if its initial value is `undefined`.
36
+
35
37
:::
36
38
37
39
## TypeScript
@@ -215,7 +217,7 @@ store.$patch((state) => {
215
217
216
218
<!-- TODO: disable this with `strictMode`, `{ noDirectPatch: true }` -->
217
219
218
-
The main difference here is that `$patch()` allows you to group multiple changes into one single entry in the devtools. Note that **both direct changes to `state` and `$patch()`appear in the devtools** and can be time traveled (not yet in Vue 3).
220
+
The main difference here is that `$patch()` allows you to group multiple changes into one single entry in the devtools. Note that **both direct changes to `state` and `$patch()`are tracked in devtools** and can be time traveled.
// note the same `pinia` instance can be used across multiple Vue apps on
49
-
// the same page
50
-
pinia,
51
-
})
52
-
```
53
-
54
-
This will also add devtools support. In Vue 3, some features like time traveling and editing are still not supported because vue-devtools doesn't expose the necessary APIs yet but the devtools have way more features and the developer experience as a whole is far superior.
55
-
56
36
## What is a Store?
57
37
58
38
A Store (like Pinia) is an entity holding state and business logic that isn't bound to your Component tree. In other words, **it hosts global state**. It's a bit like a component that is always there and that everybody can read off and write to. It has **three concepts**, the [state](./core-concepts/state.md), [getters](./core-concepts/getters.md) and [actions](./core-concepts/actions.md) and it's safe to assume these concepts are the equivalent of `data`, `computed` and `methods` in components.
Copy file name to clipboardExpand all lines: packages/docs/introduction.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@
11
11
title="Create your own Pinia from scratch"
12
12
/>
13
13
14
-
Pinia [started](https://github.com/vuejs/pinia/commit/06aeef54e2cad66696063c62829dac74e15fd19e) as an experiment to redesign what a Store for Vue could look like with the [Composition API](https://github.com/vuejs/composition-api) around November 2019. Since then, the initial principles have remained the same, but Pinia works for both Vue 2 and Vue 3 **and doesn't require you to use the composition API**. The API is the same for both except for _installation_ and _SSR_, and these docs are targeted to Vue 3 **with notes about Vue 2** whenever necessary so it can be read by Vue 2 and Vue 3 users!
14
+
Pinia [started](https://github.com/vuejs/pinia/commit/06aeef54e2cad66696063c62829dac74e15fd19e) as an experiment to redesign what a Store for Vue could look like with the [Composition API](https://github.com/vuejs/composition-api) around November 2019. Since then, the initial principles have remained the same and Vue 2 support has been dropped in 2025, but Pinia **doesn't require you to use the composition API**.
0 commit comments