Skip to content

Commit ece540a

Browse files
Modulized VUEX stores and changed to namspaced states
1 parent fe9720b commit ece540a

File tree

18 files changed

+167
-44
lines changed

18 files changed

+167
-44
lines changed

src/components/home/feed/Feed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default class Feed extends Vue {
99
private layout: number[] = [2, 2, 1, 2, 2, 3, 3, 3, 3, 3, 3];
1010
private page: number = 1;
1111

12-
@State('articles') articles!: IArticle[];
12+
@namespace('app').State('articles') articles!: IArticle[];
1313

1414
get pages() {
1515
return Math.ceil(this.articles.length / 11);

src/components/home/newest_posts/NewestPosts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import { IArticle } from '@/data/articles';
44

55
@Component({})
66
export default class NewestPosts extends Vue {
7-
@State('articles') articles!: IArticle[];
7+
@namespace('app').State('articles') articles!: IArticle[];
88
};

src/components/home/tags/Tags.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import { namespace, State, Getter } from 'vuex-class';
33

44
@Component({})
55
export default class Tags extends Vue {
6-
@Getter('categories') categories!: any;
6+
@namespace('app').Getter('categories') categories!: any;
77
};

src/pages/generic/drawer/Drawer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { namespace, State, Getter, Mutation } from 'vuex-class';
33

44
@Component({ name: 'CoreDrawer' })
55
export default class Drawer extends Vue {
6-
@Getter('links') links: any;
7-
@State('drawer') drawer: any;
8-
@Mutation('setDrawer') setDrawer: any;
6+
@namespace('app').Getter('links') links: any;
7+
@namespace('app').State('drawer') drawer: any;
8+
@namespace('app').Mutation('setDrawer') setDrawer: any;
99

1010
private onClickListItem(e: any, item: any) {
1111
e.stopPropagation();

src/pages/generic/toolbar/Toolbar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { namespace, State, Getter, Mutation } from 'vuex-class';
33

44
@Component({})
55
export default class Toolbar extends Vue {
6-
@Getter('links') links!: any;
7-
@Mutation('toggleDrawer') toggleDrawer!: any;
6+
@namespace('app').Getter('links') links!: any;
7+
@namespace('app').Mutation('toggleDrawer') toggleDrawer!: any;
88

99
private onClickButton(e: Event, item: any) {
1010
e.stopPropagation();

src/plugins/store/actions.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/plugins/store/index.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import Vue from 'vue';
22
import Vuex, { StoreOptions } from 'vuex';
3-
import { state } from './state';
4-
import { mutations } from './mutations';
5-
import { actions } from './actions';
6-
import { getters } from './getters';
3+
import app from './modules/app';
74

5+
// register VUEX
86
Vue.use(Vuex);
97

108
let opt: StoreOptions < any > = {
11-
state: state,
12-
mutations: mutations,
13-
actions: actions,
14-
getters: getters,
15-
strict: true
9+
// strict mode
10+
strict: true,
11+
12+
// module states
13+
modules: { app },
14+
15+
// root states
16+
state: {},
17+
mutations: {},
18+
actions: {},
19+
getters: {}
20+
1621
};
1722

1823
export default new Vuex.Store(opt);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import state from './modules/state';
2+
import mutations from './modules/mutations';
3+
import actions from './modules/actions';
4+
import getters from './modules/getters';
5+
6+
const app = {
7+
namespaced: true, // 成為帶名稱空間的模組
8+
state: state,
9+
mutations: mutations,
10+
actions: actions,
11+
getters: getters
12+
};
13+
14+
export default app;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Store } from 'vuex';
2+
import { State } from './state';
3+
4+
const actions = {
5+
6+
};
7+
8+
export default actions;

src/plugins/store/getters.ts renamed to src/plugins/store/modules/app/modules/getters.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
import { IState } from './state';
1+
import { State } from './state';
22
import { GetterTree, Getter } from 'vuex';
33

44
export interface IGetters {
55
[key: string]: any
66
}
77

8-
export const getters: GetterTree < IState, Getter < string, any > > = {
9-
categories: (state: IState) => {
10-
const categories: ICategory[] = [];
8+
export interface Category {
9+
text: any;
10+
to: string;
11+
}
12+
13+
const getters: GetterTree < State, Getter < string, any > > = {
14+
categories: (state: State) => {
15+
const categories: Category[] = [];
1116

1217
for (const article of state.articles) {
1318
if (
@@ -25,12 +30,9 @@ export const getters: GetterTree < IState, Getter < string, any > > = {
2530

2631
return categories.sort().slice(0, 4);
2732
},
28-
links: (state: IState, getters: IGetters) => {
33+
links: (state: State, getters: IGetters) => {
2934
return state.items.concat(getters.categories);
3035
}
3136
};
3237

33-
export interface ICategory {
34-
text: any;
35-
to: string;
36-
}
38+
export default getters;

0 commit comments

Comments
 (0)