Skip to content

Commit 79ae826

Browse files
Season ChenSeason Chen
authored andcommitted
first stable version
1 parent 1658187 commit 79ae826

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed

README.md

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,63 @@
22

33
Use [Pinia](https://github.com/vuejs/pinia) more flexibly!
44

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+
562
## Define `Store Creator`
663

764
> stores/appStore.ts
@@ -21,10 +78,10 @@ export const AppStore = ({ useStoreId }: InjectionContext) => {
2178
> App.vue
2279
```vue
2380
<script setup>
24-
import { AppStore } from '@/stores/appStore';
2581
import { provideStores, useStore } from 'pinia-di';
82+
import { AppStore } from '@/stores/appStore';
2683
27-
provideStores({ stores: [AppStore] }, name: 'app');
84+
provideStores({ stores: [AppStore] }, name: 'App');
2885
// can use by self
2986
const appStore = useStore(AppStore)();
3087
</script>
@@ -35,8 +92,8 @@ const appStore = useStore(AppStore)();
3592
> Component.vue
3693
```vue
3794
<script setup>
38-
import { AppStore } from '@/stores/appStore';
3995
import { useStore } from 'pinia-di';
96+
import { AppStore } from '@/stores/appStore';
4097
4198
const appStore = useStore(AppStore)();
4299
</script>
@@ -60,9 +117,9 @@ export const useMessageStore = MessageStore();
60117
> App.vue
61118
```vue
62119
<script setup>
120+
import { provideStores, useStore } from 'pinia-di';
63121
import { AppStore } from '@/stores/appStore';
64122
import { useMessageStore, MessageStore } from '@/stores/messageStore';
65-
import { provideStores, useStore } from 'pinia-di';
66123
67124
provideStores({ stores: [AppStore, { creator: MessageStore, use: useMessageStore }] }, name: 'App');
68125
// can use by self
@@ -73,8 +130,8 @@ const appStore = useStore(AppStore)();
73130
> Component.vue
74131
```vue
75132
<script setup>
76-
import { MessageStore } from '@/stores/messageStore';
77133
import { useStore } from 'pinia-di';
134+
import { MessageStore } from '@/stores/messageStore';
78135
79136
const messageStore = useStore(MessageStore)();
80137
</script>
@@ -139,8 +196,8 @@ If same `store creator` provided by more than one parent, the `useStore` will ge
139196
</template>
140197
141198
<script setup>
142-
import { TestStore } from '@/stores/testStore';
143199
import { provideStores } from 'pinia-di';
200+
import { TestStore } from '@/stores/testStore';
144201
145202
provideStores({ stores: [TestStore] }, name: 'ParentA');
146203
</script>
@@ -153,8 +210,8 @@ provideStores({ stores: [TestStore] }, name: 'ParentA');
153210
</template>
154211
155212
<script setup>
156-
import { TestStore } from '@/stores/testStore';
157213
import { provideStores } from 'pinia-di';
214+
import { TestStore } from '@/stores/testStore';
158215
159216
provideStores({ stores: [TestStore] }, name: 'ParentB');
160217
</script>
@@ -163,8 +220,8 @@ provideStores({ stores: [TestStore] }, name: 'ParentB');
163220
> Child.Vue
164221
```vue
165222
<script setup>
166-
import { TestStore } from '@/stores/testStore';
167223
import { useStore } from 'pinia-di';
224+
import { TestStore } from '@/stores/testStore';
168225
169226
// will get the store provided by ParentB
170227
const testStore = useStore(TestStore)();
@@ -182,7 +239,7 @@ const testStore = useStore(TestStore)();
182239
</template>
183240
184241
<script setup>
185-
import { AppStore } from '@/stores/appStore';
186242
import { StoreProvider } from 'pinia-di';
243+
import { AppStore } from '@/stores/appStore';
187244
</script>
188245
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pinia-di",
3-
"version": "0.0.4",
3+
"version": "1.0.0",
44
"description": "DI(dependency-injection) for pinia. work with vue@3",
55
"main": "dist/pinia-di.js",
66
"module": "dist/pinia-di.esm.js",

0 commit comments

Comments
 (0)