Skip to content

Commit b53632f

Browse files
committed
Merge branch 'develop'
# Conflicts: # docs/en/Vuex.md # docs/zh-cn/Vuex.md
2 parents ffb48cb + 88b6c6f commit b53632f

File tree

10 files changed

+169
-93
lines changed

10 files changed

+169
-93
lines changed

docs/en/Vuex.md

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ By using vue-cli start a project with Vuex, will generate a todo list example by
66

77
Template use [vuex-class](https://github.com/ktsn/vuex-class) to simplify vuex. Before use vuex, please read [vuex](https://vuex.vuejs.org/) for more information.
88

9-
### Define state
9+
### Define root state
1010

11-
The first step to use vuex is defining state.
11+
The first step to use vuex is defining root state.
1212

1313
```typescript
1414
// typings/interface/state.d.ts
@@ -18,24 +18,31 @@ export namespace State {
1818
export interface RootState {
1919
[key: string]: any
2020
}
21-
22-
// todo state
23-
export interface TodoState {
24-
filter: string
25-
todos: Types.todo.TodoItem[]
26-
}
2721
}
2822
```
2923

3024
### Write vuex module
3125

3226
```typescript
3327
import Vuex from 'vuex'
34-
import { State, Getter, Mutation, Action, namespace } from 'vuex-class'
3528
import keymirror from '../utils/keymirror'
3629

37-
// Use vuexUtil methods to write getter, mutation 和 action
38-
import { getter, mutation, action } from '../utils/vuexUtil'
30+
import {
31+
State as vState,
32+
Getter as vGetter,
33+
Mutation as vMutation,
34+
Action as vAction,
35+
namespace
36+
} from 'vuex-class'
37+
38+
// Use vuexUtil methods to write getter, mutation and action
39+
import {
40+
getter,
41+
mutation,
42+
action,
43+
decorator
44+
} from '../utils/vuexUtil'
45+
3946

4047
/*** state ***/
4148
let state: TodoState = {}
@@ -72,12 +79,12 @@ export let types = {
7279
action: keymirror(actions)
7380
}
7481

75-
export let module = {
76-
State: namespace('todo', State),
77-
Getter: namespace('todo', Getter),
78-
Mutation: namespace('todo', Mutation),
79-
Action: namespace('todo', Action)
80-
}
82+
83+
const storeName = 'todo'
84+
export let State = decorator(namespace(storeName, vState), types.state)
85+
export let Getter = decorator(namespace(storeName, vGetter), types.getter)
86+
export let Mutation = decorator(namespace(storeName, vMutation), types.mutation)
87+
export let Action = decorator(namespace(storeName, vAction), types.action)
8188

8289
export default store
8390
```
@@ -98,17 +105,24 @@ const store = new Vuex.Store({
98105
### Use in components
99106

100107
```typescript
101-
// vuex
102-
import { types, module } from 'store/modules/todo'
108+
import { State, Getter, Mutation, Action } from 'store/modules/todo'
103109

104110
class Todo extends Vue {
105-
@module.State(types.state.todos) allTodos
106-
107-
@module.Getter(types.getter.filterTodos) todos
108-
109-
@module.Mutation(types.mutation.setFilter) setFilter
110-
111-
@module.Action(types.action.fetch) fetch
111+
@State('todos')
112+
allTodos: Types.todo.Item[]
113+
114+
// == @State('foo') foo: string
115+
@State
116+
foo: string
117+
118+
@Getter('filterTodos')
119+
todos: Types.todo.Item[]
120+
121+
@Mutation
122+
filterTodos: (filter: string) => void
123+
124+
@Action
125+
fetch: () => Promise<any>
112126
}
113127
```
114128

docs/zh-cn/Vuex.md

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
模板使用了 [vuex-class](https://github.com/ktsn/vuex-class) 简化 vuex。使用 vuex 之前,请先了解 [vuex](https://vuex.vuejs.org/)
88

9-
### 定义模块 state
9+
### 定义模块 RootState
1010

11-
使用 vuex 的第一步是先定义模块的 state 有哪一些
11+
使用 vuex 的第一步是先定义 RootState
1212

1313
```typescript
1414
// typings/interface/state.d.ts
@@ -18,24 +18,31 @@ export namespace State {
1818
export interface RootState {
1919
[key: string]: any
2020
}
21-
22-
// todo state
23-
export interface TodoState {
24-
filter: string
25-
todos: Types.todo.TodoItem[]
26-
}
2721
}
2822
```
2923

3024
### 编写 vuex 模块
3125

3226
```typescript
3327
import Vuex from 'vuex'
34-
import { State, Getter, Mutation, Action, namespace } from 'vuex-class'
3528
import keymirror from '../utils/keymirror'
3629

37-
// 使用 vuexUtil 提供的方法编写 getter, mutation 和 action
38-
import { getter, mutation, action } from '../utils/vuexUtil'
30+
import {
31+
State as vState,
32+
Getter as vGetter,
33+
Mutation as vMutation,
34+
Action as vAction,
35+
namespace
36+
} from 'vuex-class'
37+
38+
// Use vuexUtil methods to write getter, mutation and action
39+
import {
40+
getter,
41+
mutation,
42+
action,
43+
decorator
44+
} from '../utils/vuexUtil'
45+
3946

4047
/*** state ***/
4148
let state: TodoState = {}
@@ -72,14 +79,15 @@ export let types = {
7279
action: keymirror(actions)
7380
}
7481

75-
export let module = {
76-
State: namespace('todo', State),
77-
Getter: namespace('todo', Getter),
78-
Mutation: namespace('todo', Mutation),
79-
Action: namespace('todo', Action)
80-
}
82+
83+
const storeName = 'todo'
84+
export let State = decorator(namespace(storeName, vState), types.state)
85+
export let Getter = decorator(namespace(storeName, vGetter), types.getter)
86+
export let Mutation = decorator(namespace(storeName, vMutation), types.mutation)
87+
export let Action = decorator(namespace(storeName, vAction), types.action)
8188

8289
export default store
90+
8391
```
8492

8593
### 在主 store 中加入该模块
@@ -99,18 +107,26 @@ const store = new Vuex.Store({
99107

100108
```typescript
101109
// vuex
102-
import { types, module } from 'store/modules/todo'
110+
import { State, Getter, Mutation, Action } from 'store/modules/todo'
103111

104112
class Todo extends Vue {
105-
@module.State(types.state.todos) allTodos
106-
107-
@module.Getter(types.getter.filterTodos) todos
108-
109-
@module.Mutation(types.mutation.setFilter) setFilter
110-
111-
@module.Action(types.action.fetch) fetch
113+
@State('todos')
114+
allTodos: Types.todo.Item[]
115+
116+
// == @State('foo') foo: string
117+
@State
118+
foo: string
119+
120+
@Getter('filterTodos')
121+
todos: Types.todo.Item[]
122+
123+
@Mutation
124+
filterTodos: (filter: string) => void
125+
126+
@Action
127+
fetch: () => Promise<any>
112128
}
113-
```
114-
115129

130+
```
131+
建议 state, getter, mutation, action 需要明确写出其类型。
116132

template/src/components/views/todo/filter/filter.vue

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
-->
77
<template>
88
<div class="filters">
9-
<!-- <button class="cur">All</button>
10-
<button>Active</button>
11-
<button>Completed</button> -->
129
<router-link to="/todo/all" active-class="cur">All</router-link>
1310
<router-link to="/todo/active" active-class="cur">Active</router-link>
1411
<router-link to="/todo/completed" active-class="cur">Completed</router-link>

template/src/components/views/todo/footer/footer.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import template from './footer.vue'
1111

1212
import Filters from '../filter'
1313

14-
import { types, module} from 'store/modules/todo'
14+
import { Getter, Mutation } from 'store/modules/todo'
1515

1616
@Component({
1717
name: 'tag-todo-footer',
@@ -21,8 +21,12 @@ import { types, module} from 'store/modules/todo'
2121
}
2222
})
2323
export default class Footer extends Vue {
24-
@module.Getter(types.getter.remaining) remaining: Types.todo.TodoItem[]
25-
@module.Getter(types.getter.completed) completed: Types.todo.TodoItem[]
24+
@Getter
25+
remaining: Types.todo.Item[]
2626

27-
@module.Mutation(types.mutation.clearComplete) clearComplete
27+
@Getter
28+
completed: Types.todo.Item[]
29+
30+
@Mutation
31+
clearComplete: () => void
2832
}

template/src/components/views/todo/inputBox/inputBox.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Vue from 'components/base'
99
import { Component } from 'vue-property-decorator'
1010
import template from './inputBox.vue'
1111

12-
import { types, module } from 'store/modules/todo'
12+
import { State, Getter, Mutation } from 'store/modules/todo'
1313

1414
@Component({
1515
name: 'tag-todo-inputbox',
@@ -18,12 +18,17 @@ import { types, module } from 'store/modules/todo'
1818
export default class InputBox extends Vue {
1919
title = ''
2020

21-
@module.State(types.state.todos) todos
21+
@State
22+
todos: Types.todo.Item[]
2223

23-
@module.Getter(types.getter.isAllCompleted) isAllCompleted
24+
@Getter
25+
isAllCompleted: boolean
2426

25-
@module.Mutation(types.mutation.addTodo) addTodo
26-
@module.Mutation(types.mutation.toggleAllTodoStatus) toggleAllStatus
27+
@Mutation
28+
addTodo: (todo: Types.todo.Item) => void
29+
30+
@Mutation('toggleAllTodoStatus')
31+
toggleAllStatus: (status: boolean) => void
2732

2833
onEnter () {
2934
this.addTodo({

template/src/components/views/todo/item/item.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@ import Vue from 'components/base'
99
import { Component, Prop } from 'vue-property-decorator'
1010
import template from './item.vue'
1111

12-
import { types, module } from 'store/modules/todo'
12+
import { Mutation } from 'store/modules/todo'
1313

1414
@Component({
1515
name: 'tag-todo-item',
1616
mixins: [template]
1717
})
1818
export default class Item extends Vue {
1919
@Prop()
20-
todo: Types.todo.TodoItem
20+
todo: Types.todo.Item
21+
22+
@Mutation('removeTodo')
23+
remove: (todo: Types.todo.Item) => void
24+
25+
@Mutation('toggleTodoStatus')
26+
toggleStatus: (payload: { todo: Types.todo.Item, status: boolean }) => void
2127

2228
get status () {
2329
return this.todo.completed
@@ -29,7 +35,4 @@ export default class Item extends Vue {
2935
status: value
3036
})
3137
}
32-
33-
@module.Mutation(types.mutation.removeTodo) remove
34-
@module.Mutation(types.mutation.toggleTodoStatus) toggleStatus
3538
}

template/src/components/views/todo/todo.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import TodoItem from './item'
1414
import TodoFooter from './footer'
1515

1616
// vuex
17-
import { types, module } from 'store/modules/todo'
17+
import { State, Getter, Mutation, Action } from 'store/modules/todo'
1818

1919
@Component({
2020
name: 'view-todo',
@@ -26,25 +26,33 @@ import { types, module } from 'store/modules/todo'
2626
}
2727
})
2828
export default class Todo extends Vue {
29-
@module.State(types.state.todos) allTodos
29+
@State('todos')
30+
allTodos: Types.todo.Item[]
3031

31-
@module.Getter(types.getter.filterTodos) todos
32+
@Getter('filterTodos')
33+
todos: Types.todo.Item[]
3234

33-
@module.Mutation(types.mutation.setFilter) setFilter
34-
@module.Mutation(types.mutation.clearData) clearData
35+
@Mutation
36+
setFilter: (filter) => void
3537

36-
@module.Action(types.action.fetch) fetch
37-
@module.Action(types.action.save) save
38+
@Mutation
39+
clearData: () => void
40+
41+
@Action
42+
fetch: () => Promise<any>
43+
44+
@Action
45+
save: () => Promise<any>
3846

3947
@Watch('todos', {deep: true})
4048
onTodosChange () {
4149
// save todos
4250
this.save()
4351
}
4452

45-
created () {
53+
async created () {
4654
// fetch todos
47-
this.fetch()
55+
await this.fetch()
4856

4957
if (this.$route.params && this.$route.params.filter) {
5058
this.setFilter(this.$route.params.filter)

0 commit comments

Comments
 (0)