Skip to content

Commit 387d54b

Browse files
committed
docs: notes about vue 3 and 2
1 parent 7165006 commit 387d54b

File tree

6 files changed

+10
-96
lines changed

6 files changed

+10
-96
lines changed

packages/docs/cookbook/testing.md

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ expect(store.someAction).toHaveBeenCalledTimes(1)
109109
expect(store.someAction).toHaveBeenLastCalledWith()
110110
```
111111

112-
Please note that if you are using Vue 2, `@vue/test-utils` requires a [slightly different configuration](#Unit-test-components-Vue-2-).
113-
114112
### Initial State
115113

116114
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:
@@ -291,23 +289,3 @@ const wrapper = mount(Counter, {
291289
## E2E tests
292290

293291
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'
302-
import { createTestingPinia } from '@pinia/testing'
303-
304-
const localVue = createLocalVue()
305-
localVue.use(PiniaVuePlugin)
306-
307-
const wrapper = mount(Counter, {
308-
localVue,
309-
pinia: createTestingPinia(),
310-
})
311-
312-
const store = useSomeStore() // uses the testing pinia!
313-
```

packages/docs/core-concepts/plugins.md

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ A Pinia plugin is a function that optionally returns properties to be added to a
4444
```js
4545
export function myPiniaPlugin(context) {
4646
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()`
4848
context.store // the store the plugin is augmenting
4949
context.options // the options object defining the store passed to `defineStore()`
5050
// ...
@@ -143,28 +143,6 @@ pinia.use(({ store }) => {
143143

144144
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**.
145145

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-
const secretRef = 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`
161-
set(store, 'secret', toRef(store.$state, 'secret'))
162-
store.secret // 'secret'
163-
})
164-
```
165-
166-
:::
167-
168146
#### Resetting state added in plugins
169147

170148
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' {
395373
```
396374

397375
:::tip
376+
398377
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+
399379
:::
400380

401-
## Nuxt.js
381+
## Nuxt
402382

403383
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:
404384

@@ -427,32 +407,6 @@ The above example is using TypeScript, you have to remove the type annotations `
427407

428408
:::
429409

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) {
440-
store.$subscribe((mutation) => {
441-
// react to store changes
442-
console.log(`[🍍 ${mutation.storeId}]: ${mutation.type}.`)
443-
})
444-
445-
// Note this has to be typed if you are using TS
446-
return { creationTime: new Date() }
447-
}
448-
449-
const myPlugin: Plugin = ({ $pinia }) => {
450-
$pinia.use(MyPiniaPlugin)
451-
}
452-
453-
export default myPlugin
454-
```
455-
456410
## Existing plugins
457411

458412
You can check existing [Pinia plugins on GitHub](https://github.com/topics/pinia-plugin) with the topic _pinia-plugin_.

packages/docs/core-concepts/state.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export const useStore = defineStore('storeId', {
3131
```
3232

3333
:::tip
34-
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+
3537
:::
3638

3739
## TypeScript
@@ -215,7 +217,7 @@ store.$patch((state) => {
215217

216218
<!-- TODO: disable this with `strictMode`, `{ noDirectPatch: true }` -->
217219

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.
219221

220222
## Replacing the `state`
221223

packages/docs/getting-started.md

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,6 @@ app.use(pinia)
3333
app.mount('#app')
3434
```
3535

36-
If you are using Vue 2, you also need to install a plugin and inject the created `pinia` at the root of the app:
37-
38-
```js {1,3-4,12}
39-
import { createPinia, PiniaVuePlugin } from 'pinia'
40-
41-
Vue.use(PiniaVuePlugin)
42-
const pinia = createPinia()
43-
44-
new Vue({
45-
el: '#app',
46-
// other options...
47-
// ...
48-
// 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-
5636
## What is a Store?
5737

5838
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.

packages/docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ features:
3434
- title: 🔑 Type Safe
3535
details: Types are inferred, which means stores provide you with autocompletion even in JavaScript!
3636
- title: ⚙️ Devtools support
37-
details: Pinia hooks into Vue devtools to give you an enhanced development experience in both Vue 2 and Vue 3.
37+
details: Pinia hooks into Vue devtools to give you an enhanced development experience.
3838
- title: 🔌 Extensible
3939
details: React to store changes and actions to extend Pinia with transactions, local storage synchronization, etc.
4040
- title: 🏗 Modular by design

packages/docs/introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
title="Create your own Pinia from scratch"
1212
/>
1313

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**.
1515

1616
## Why should I use Pinia?
1717

0 commit comments

Comments
 (0)