Skip to content

Commit 4bd9ce0

Browse files
author
Sascha Braun
committed
update readme & samples (add examples with dependency injection for easier migration)
1 parent 500d896 commit 4bd9ce0

File tree

9 files changed

+134
-14
lines changed

9 files changed

+134
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Changelog
22

33
2.0.0:
4-
- Remove typedi / dependency injections
4+
- Remove typedi / dependency injections: now available in a separate package [vue-typedi](https://github.com/sascha245/vue-typedi)
55
- Remove deprecated functions
66
- Cleaner and easier usages
77
- Submodules

README.md

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ A simpler way to write your Vuex store in Typescript
55
## Changelog
66

77
2.0.0:
8-
- Remove typedi / dependency injections
8+
- Remove typedi / dependency injections: : now available in a separate package [vue-typedi](https://github.com/sascha245/vue-typedi)
99
- Remove deprecated functions
1010
- Cleaner and easier usages
1111
- Submodules
1212

13-
## Usage
13+
## Install
1414

1515
1. Install vuex
1616
`npm install vuex --save`
@@ -125,7 +125,7 @@ import { MyStore } from '@/store/store';
125125
@Component
126126
export default class MyComponent extends Vue {
127127

128-
public store = useStore<MyStore>(this.$store);
128+
public store: MyStore = useStore(this.$store);
129129

130130
public get readState() {
131131
// access state like a property
@@ -153,6 +153,82 @@ export default class MyComponent extends Vue {
153153
}
154154
```
155155

156+
## Example with dependency injections
157+
158+
#### Module with dependency injection
159+
160+
```ts
161+
// store/modules/foo.ts
162+
163+
import { Mutation, State } from 'vuex-simple';
164+
import { Inject, Injectable } from 'vue-typedi';
165+
import { MyService } from '...';
166+
167+
@Injectable()
168+
export class FooModule {
169+
170+
@Inject()
171+
public myService!: MyService;
172+
173+
...
174+
}
175+
```
176+
177+
#### Vue component with module injection
178+
179+
```ts
180+
// store/tokens.ts
181+
182+
import { Token } from 'vue-typedi';
183+
184+
export default {
185+
BAR: new Token('bar'),
186+
BAR_FOO1: new Token('bar/foo1'),
187+
BAR_FOO2: new Token('bar/foo2')
188+
};
189+
190+
// store/index.ts
191+
192+
import Vue from 'vue';
193+
import Vuex from 'vuex';
194+
import { Container } from 'vue-typedi';
195+
196+
import { createVuexStore } from 'vuex-simple';
197+
198+
import { MyStore } from './store';
199+
200+
import tokens from './tokens'
201+
202+
Vue.use(Vuex);
203+
204+
const instance = new MyStore()
205+
206+
Container.set(instance.bar, tokens.BAR);
207+
Container.set(instance.bar.foo1, tokens.BAR_FOO1);
208+
Container.set(instance.bar.foo2, tokens.BAR_FOO2);
209+
210+
export default createVuexStore(instance, {
211+
strict: false,
212+
modules: {},
213+
plugins: []
214+
});
215+
216+
// In your vue component
217+
218+
import { Inject } from 'vue-typedi';
219+
import { FooModule } from '@/store/modules/foo';
220+
import tokens from '@/store/tokens';
221+
222+
@Component
223+
export default class MyComponent extends Vue {
224+
225+
@Inject(tokens.BAR_FOO1)
226+
public foo1!: FooModule;
227+
228+
...
229+
}
230+
```
231+
156232
## Features
157233

158234
#### State

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"vue-property-decorator": "^7.0.0",
4040
"vue-router": "^3.0.1",
4141
"vue-template-compiler": "^2.5.17",
42+
"vue-typedi": "^1.1.1",
4243
"vuex": "^3.0.1"
4344
},
4445
"browserslist": [

samples/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import 'reflect-metadata';
22

33
import Vue from 'vue';
4+
import VueTypedi from 'vue-typedi';
45

56
import App from './App.vue';
67
import router from './router';
78
import store from './store';
89

910
Vue.config.productionTip = false;
11+
Vue.use(VueTypedi);
1012

1113
new Vue({
1214
render: h => h(App),

samples/store/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import Vue from 'vue';
2+
import { Container } from 'vue-typedi';
23
import Vuex from 'vuex';
34

45
import { createVuexStore } from '../../src';
56
import { MyStore } from './store';
7+
import tokens from './tokens';
68

79
Vue.use(Vuex);
810

9-
export default createVuexStore(new MyStore(), {
11+
const instance = new MyStore();
12+
13+
Container.set(tokens.MY, instance.my);
14+
Container.set(tokens.TEST, instance.test);
15+
Container.set(tokens.TEST_MY1, instance.test.my1);
16+
Container.set(tokens.TEST_MY2, instance.test.my2);
17+
18+
export default createVuexStore(instance, {
1019
strict: true
1120
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Inject, Injectable } from 'vue-typedi';
2+
3+
import { Getter, Module } from '../../../src';
4+
import { MyModule } from './MyModule';
5+
import { TestModule } from './TestModule';
6+
7+
@Injectable()
8+
export class InjectableModule {
9+
@Inject()
10+
public testModule!: TestModule;
11+
12+
@Module()
13+
public my1 = new MyModule(5);
14+
15+
@Module()
16+
public my2 = new MyModule();
17+
18+
@Getter()
19+
public get total() {
20+
return this.my1.counter + this.my2.counter;
21+
}
22+
}

samples/store/services/TestService.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { Service } from "typedi";
1+
import { Service } from 'vue-typedi';
22

33
@Service()
44
export class TestService {
5-
private _something = 42;
6-
75
public async countItems() {
86
await new Promise(r => setTimeout(r, 500));
97
return 42;

samples/store/tokens.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Token } from 'vue-typedi';
2+
3+
export default {
4+
MY: new Token('my'),
5+
TEST: new Token('test'),
6+
TEST_MY1: new Token('test/my1'),
7+
TEST_MY2: new Token('test/my2')
8+
};

samples/views/Home.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import { Component, Vue } from 'vue-property-decorator';
2+
import { Inject } from 'vue-typedi';
23

34
import { useStore } from '../../src';
5+
import { TestModule } from '../store/modules/TestModule';
46
import { MyStore } from '../store/store';
7+
import tokens from '../store/tokens';
58

69
@Component
710
export default class Home extends Vue {
8-
public store = useStore<MyStore>(this.$store);
11+
public store: MyStore = useStore(this.$store);
912

10-
public get testModule() {
11-
return this.store.test;
12-
}
13+
@Inject(tokens.TEST)
14+
public testModule!: TestModule;
15+
16+
// public get testModule() {
17+
// return this.store.test;
18+
// }
1319

1420
public get counter() {
1521
return this.testModule.counter;
@@ -21,6 +27,4 @@ export default class Home extends Vue {
2127
public increment() {
2228
this.testModule.increment();
2329
}
24-
25-
public created() {}
2630
}

0 commit comments

Comments
 (0)