Skip to content

Commit

Permalink
doc: update pinia.md jaywcjlove#672
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 24, 2024
1 parent 819704c commit 1fb3dd7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 49 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ Quick Reference
[RegEx 正则表达式](./docs/regex.md)<!--rehype:style=background: rgb(149 36 155);-->
[TypeScript](./docs/typescript.md)<!--rehype:style=background: rgb(49 120 198);-->
[Tauri](./docs/tauri.md)<!--rehype:style=background: rgb(103 214 237);&class=contributing-->
[Vue 2](./docs/vue2.md)<!--rehype:style=background: rgb(64 184 131);-->
[Vue 3](./docs/vue.md)<!--rehype:style=background: rgb(64 184 131);&class=contributing-->
[Vue 2](./docs/vue2.md)<!--rehype:style=background: rgb(64 184 131);&class=tag&data-lang=Vue-->
[Vue 3](./docs/vue.md)<!--rehype:style=background: rgb(64 184 131);&class=tag&class=contributing tag&data-lang=Vue-->
[</> htmx](./docs/htmx.md)<!--rehype:style=background: rgb(52 101 164);&class=contributing-->
[Pinia](./docs/pinia.md)<!--rehype:style=background: rgb(44 136 50);&class=tag&data-lang=Vue-->
<!--rehype:class=home-card-->

## CSS
Expand Down
1 change: 1 addition & 0 deletions assets/pinia.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
102 changes: 55 additions & 47 deletions docs/pinia.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,10 @@ yarn add pinia
pnpm add pinia
```

### 创建 Pinia 实例

在你的 Vue 应用中创建一个 Pinia 实例并将其传递给 Vue:

```javascript
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'

const app = createApp(App)
const pinia = createPinia()

app.use(pinia)
app.mount('#app')
```

### 定义 Store
<!--rehype:wrap-class=col-span-2 row-span-2-->

创建一个 store 文件(例如 `src/stores/counter.js`),并定义 store
创建一个 store 文件(例如 `src/stores/counter.js`),并定义 `store`

```javascript
import { defineStore } from 'pinia'
Expand All @@ -54,9 +39,36 @@ export const useCounterStore = defineStore('counter', {
})
```

### 创建 Pinia 实例

```javascript
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'

const app = createApp(App)
const pinia = createPinia()

app.use(pinia)
app.mount('#app')
```

在你的 [Vue](./vue.md) 应用中创建一个 Pinia 实例并将其传递给 [Vue](./vue.md)

### 热重载 Store

使用 Vite 时,你可以启用热重载功能:

```javascript
if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useCounterStore, import.meta.hot))
}
```
### 使用 Store
<!--rehype:wrap-class=row-span-2-->
在组件中使用 store
在组件中使用 `store`
```javascript
<template>
Expand All @@ -82,17 +94,8 @@ export default {
</script>
```
### 热重载 Store

使用 Vite 时,你可以启用热重载功能:

```javascript
if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useCounterStore, import.meta.hot))
}
```
### Modules 模式
<!--rehype:wrap-class=row-span-2-->
Pinia 不使用传统的 Vuex 模块模式。相反,推荐使用独立的 store 文件:
Expand Down Expand Up @@ -160,6 +163,7 @@ export function useCounter() {
```
### 插件
<!--rehype:wrap-class=col-span-2-->
Pinia 支持插件。你可以编写插件来扩展 Pinia 的功能:
Expand All @@ -179,37 +183,22 @@ const pinia = createPinia()
pinia.use(piniaPlugin)
```
### SSR 支持
Pinia 支持服务端渲染 (SSR)。在你的 SSR 入口文件中创建 Pinia 实例:
```javascript
import { createPinia } from 'pinia'

export function createApp() {
const app = createSSRApp(App)
const pinia = createPinia()

app.use(pinia)
return { app, pinia }
}
```
明白了,让我们来结合 `pinia-plugin-persist` 插件完善 Pinia 备忘清单。
### 持久化状态
<!--rehype:wrap-class=row-span-4 col-span-2-->
#### 1. 安装 `pinia-plugin-persist`
<!--rehype:style=color:#228e6c;font-weight: bold;text-align: left;-->
```bash
npm pinia-plugin-persist
```
#### 2. 配置 Pinia 和 `pinia-plugin-persist`
<!--rehype:style=color:#228e6c;font-weight: bold;text-align: left;-->
在你的入口文件中配置 Pinia 和 `pinia-plugin-persist`
**Vue 2 项目**
**⚠️ Vue 2 项目**
```javascript
import Vue from 'vue'
Expand Down Expand Up @@ -247,6 +236,7 @@ createApp(App)
```
#### 3. 创建 Store 并启用持久化
<!--rehype:style=color:#228e6c;font-weight: bold;text-align: left;-->
创建一个 Pinia store,并启用持久化存储。
Expand Down Expand Up @@ -278,6 +268,7 @@ export const useUserStore = defineStore('userStore', {
```
#### 4. 使用 Store
<!--rehype:style=color:#228e6c;font-weight: bold;text-align: left;-->
在组件中使用创建好的 store。
Expand All @@ -303,6 +294,23 @@ export default {
</script>
```
### SSR 支持
Pinia 支持服务端渲染 (SSR)。在你的 SSR 入口文件中创建 Pinia 实例:
```javascript
import { createPinia } from 'pinia'

export function createApp() {
const app = createSSRApp(App)
const pinia = createPinia()

app.use(pinia)
return { app, pinia }
}
```
明白了,让我们来结合 `pinia-plugin-persist` 插件完善 Pinia 备忘清单。
### 使用 Vue Devtools
Expand Down

0 comments on commit 1fb3dd7

Please sign in to comment.