2
2
3
3
Use [ Pinia] ( https://github.com/vuejs/pinia ) more flexibly!
4
4
5
+ ## Essentials
6
+
7
+ - ` Store Use ` : The ` return ` of [ defineStore] ( https://pinia.vuejs.org/core-concepts/#defining-a-store ) .
8
+ - ` Store Creator ` : A function that return a ` Store Use ` .
9
+ - ` InjectionContext ` : The parameter that the ` Store Creator ` will receive.
10
+
11
+ ## InjectionContext: ` { getStore, useStoreId, onUnmounted } `
12
+
13
+ ` getStore ` : Get other store that have been provided by parent component or self component`.
14
+ ``` ts
15
+ import { InjectionContext } from ' pinia-di' ;
16
+ import { OtherStore } from ' ./stores/other' ;
17
+
18
+ export const AppStore = ({ getStore }: InjectionContext ) => {
19
+ return defineStore (' app' , {
20
+ state: {},
21
+ actions: {
22
+ test() {
23
+ const otherStore = getStore (OtherStore );
24
+ console .log (otherStore .xx );
25
+ }
26
+ }
27
+ });
28
+ }
29
+ ```
30
+
31
+ ` useStoreId ` : Because ` pinia ` use ` id ` to identify one store, but our ` Store Creator ` maybe use multiple times, so we need a method ` useStoreId ` to generate the unique id.
32
+ ``` ts
33
+ import { InjectionContext } from ' pinia-di' ;
34
+ export const TestStore = ({ useStoreId }: InjectionContext ) => {
35
+ return defineStore (useStoreId (' test' ), {
36
+ state: {},
37
+ });
38
+ }
39
+ ```
40
+
41
+ ` onUnmounted ` : Bind a function that will be invoked when the store unmounted.
42
+ ``` ts
43
+ import { InjectionContext } from ' pinia-di' ;
44
+ export const TestStore = ({ onUnmounted }: InjectionContext ) => {
45
+ const useTestStore = defineStore (useStoreId (' test' ), {
46
+ state: {},
47
+ actions: {
48
+ dispose() {
49
+ console .log (' dispose' );
50
+ }
51
+ }
52
+ });
53
+
54
+ onUnmounted (() => {
55
+ useTestStore ().dispose ();
56
+ });
57
+
58
+ return useTestStore ;
59
+ }
60
+ ```
61
+
5
62
## Define ` Store Creator `
6
63
7
64
> stores/appStore.ts
@@ -21,10 +78,10 @@ export const AppStore = ({ useStoreId }: InjectionContext) => {
21
78
> App.vue
22
79
``` vue
23
80
<script setup>
24
- import { AppStore } from '@/stores/appStore';
25
81
import { provideStores, useStore } from 'pinia-di';
82
+ import { AppStore } from '@/stores/appStore';
26
83
27
- provideStores({ stores: [AppStore] }, name: 'app ');
84
+ provideStores({ stores: [AppStore] }, name: 'App ');
28
85
// can use by self
29
86
const appStore = useStore(AppStore)();
30
87
</script>
@@ -35,8 +92,8 @@ const appStore = useStore(AppStore)();
35
92
> Component.vue
36
93
``` vue
37
94
<script setup>
38
- import { AppStore } from '@/stores/appStore';
39
95
import { useStore } from 'pinia-di';
96
+ import { AppStore } from '@/stores/appStore';
40
97
41
98
const appStore = useStore(AppStore)();
42
99
</script>
@@ -60,9 +117,9 @@ export const useMessageStore = MessageStore();
60
117
> App.vue
61
118
``` vue
62
119
<script setup>
120
+ import { provideStores, useStore } from 'pinia-di';
63
121
import { AppStore } from '@/stores/appStore';
64
122
import { useMessageStore, MessageStore } from '@/stores/messageStore';
65
- import { provideStores, useStore } from 'pinia-di';
66
123
67
124
provideStores({ stores: [AppStore, { creator: MessageStore, use: useMessageStore }] }, name: 'App');
68
125
// can use by self
@@ -73,8 +130,8 @@ const appStore = useStore(AppStore)();
73
130
> Component.vue
74
131
``` vue
75
132
<script setup>
76
- import { MessageStore } from '@/stores/messageStore';
77
133
import { useStore } from 'pinia-di';
134
+ import { MessageStore } from '@/stores/messageStore';
78
135
79
136
const messageStore = useStore(MessageStore)();
80
137
</script>
@@ -139,8 +196,8 @@ If same `store creator` provided by more than one parent, the `useStore` will ge
139
196
</template>
140
197
141
198
<script setup>
142
- import { TestStore } from '@/stores/testStore';
143
199
import { provideStores } from 'pinia-di';
200
+ import { TestStore } from '@/stores/testStore';
144
201
145
202
provideStores({ stores: [TestStore] }, name: 'ParentA');
146
203
</script>
@@ -153,8 +210,8 @@ provideStores({ stores: [TestStore] }, name: 'ParentA');
153
210
</template>
154
211
155
212
<script setup>
156
- import { TestStore } from '@/stores/testStore';
157
213
import { provideStores } from 'pinia-di';
214
+ import { TestStore } from '@/stores/testStore';
158
215
159
216
provideStores({ stores: [TestStore] }, name: 'ParentB');
160
217
</script>
@@ -163,8 +220,8 @@ provideStores({ stores: [TestStore] }, name: 'ParentB');
163
220
> Child.Vue
164
221
``` vue
165
222
<script setup>
166
- import { TestStore } from '@/stores/testStore';
167
223
import { useStore } from 'pinia-di';
224
+ import { TestStore } from '@/stores/testStore';
168
225
169
226
// will get the store provided by ParentB
170
227
const testStore = useStore(TestStore)();
@@ -182,7 +239,7 @@ const testStore = useStore(TestStore)();
182
239
</template>
183
240
184
241
<script setup>
185
- import { AppStore } from '@/stores/appStore';
186
242
import { StoreProvider } from 'pinia-di';
243
+ import { AppStore } from '@/stores/appStore';
187
244
</script>
188
245
```
0 commit comments