16
16
``` js
17
17
import { defineStore } from ' pinia'
18
18
19
- // 你可以任意命名 `defineStore()` 的返回值,但最好使用 store 的名字,同时以 `use` 开头且以 `Store` 结尾。
19
+ // `defineStore()` 的返回值的命名是自由的
20
+ // 但最好含有 store 的名字,且以 `use` 开头,以 `Store` 结尾。
20
21
// (比如 `useUserStore`,`useCartStore`,`useProductStore`)
21
22
// 第一个参数是你的应用中 Store 的唯一 ID。
22
23
export const useAlertsStore = defineStore (' alerts' , {
@@ -57,12 +58,13 @@ export const useCounterStore = defineStore('counter', {
57
58
``` js
58
59
export const useCounterStore = defineStore (' counter' , () => {
59
60
const count = ref (0 )
61
+ const name = ref (' Eduardo' )
60
62
const doubleCount = computed (() => count .value * 2 )
61
63
function increment () {
62
64
count .value ++
63
65
}
64
66
65
- return { count, doubleCount, increment }
67
+ return { count, name, doubleCount, increment }
66
68
})
67
69
```
68
70
@@ -111,7 +113,7 @@ export const useSearchFilters = defineStore('search-filters', () => {
111
113
``` vue
112
114
<script setup>
113
115
import { useCounterStore } from '@/stores/counter'
114
- // 可以在组件中的任意位置访问 `store` 变量 ✨
116
+ // 在组件内部的任何地方均可以访问变量 `store` ✨
115
117
const store = useCounterStore()
116
118
</script>
117
119
```
@@ -120,7 +122,7 @@ const store = useCounterStore()
120
122
如果你还不会使用 ` setup ` 组件,[ 你也可以通过** 映射辅助函数** 来使用 Pinia] ( ../cookbook/options-api.md ) 。
121
123
:::
122
124
123
- 你可以定义任意多的 store,但为了让使用 pinia 的益处最大化 (比如允许构建工具自动进行代码分割以及 TypeScript 推断),** 你应该在不同的文件中去定义 store** 。
125
+ 你可以定义任意多的 store,但为了让使用 pinia 的益处最大化(比如允许构建工具自动进行代码分割以及 TypeScript 推断),** 你应该在不同的文件中去定义 store** 。
124
126
125
127
一旦 store 被实例化,你可以直接访问在 store 的 ` state ` 、` getters ` 和 ` actions ` 中定义的任何属性。我们将在后续章节继续了解这些细节,目前自动补全将帮助你使用相关属性。
126
128
@@ -132,16 +134,16 @@ import { useCounterStore } from '@/stores/counter'
132
134
import { computed } from 'vue'
133
135
134
136
const store = useCounterStore()
135
- // ❌ 这将不起作用,因为它破坏了响应性
136
- // 这就和直接解构 `props` 一样
137
+ // ❌ 下面这部分代码不会生效,因为它的响应式被破坏了
138
+ // 它和解构 `props` 的操作是一样的
137
139
const { name, doubleCount } = store // [!code warning]
138
- name // 将始终是 "Eduardo" // [!code warning]
139
- doubleCount // 将始终是 0 // [!code warning]
140
+ name // 将会一直是 "Eduardo" // [!code warning]
141
+ doubleCount // 将会一直是 0 // [!code warning]
140
142
setTimeout(() => {
141
143
store.increment()
142
144
}, 1000)
143
- // ✅ 这样写是响应式的
144
- // 💡 当然你也可以直接使用 `store.doubleCount`
145
+ // ✅ 而这一部分代码就会维持响应式
146
+ // 💡 在这里你也可以直接使用 `store.doubleCount`
145
147
const doubleValue = computed(() => store.doubleCount)
146
148
</script>
147
149
```
@@ -154,11 +156,11 @@ const doubleValue = computed(() => store.doubleCount)
154
156
<script setup>
155
157
import { storeToRefs } from 'pinia'
156
158
const store = useCounterStore()
157
- // `name` 和 `doubleCount` 是响应式的 ref
158
- // 同时通过插件添加的属性也会被提取为 ref
159
- // 并且会跳过所有的 action 或非响应式 (不是 ref 或 reactive) 的属性
159
+ // `name` 和 `doubleCount` 都是响应式引用
160
+ // 下面的代码同样会提取那些来自插件的属性的响应式引用
161
+ // 但是会跳过所有的 action 或者非响应式(非 ref 或者 非 reactive) 的属性
160
162
const { name, doubleCount } = storeToRefs(store)
161
- // 作为 action 的 increment 可以直接解构
163
+ // 名为 increment 的 action 可以被解构
162
164
const { increment } = store
163
165
</script>
164
166
```
0 commit comments