Skip to content

Commit 0c50029

Browse files
committed
vuex add namespaced
1 parent 7c33568 commit 0c50029

File tree

20 files changed

+401
-369
lines changed

20 files changed

+401
-369
lines changed

src/components/LangSelect/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default {
2727
methods: {
2828
handleSetLanguage(lang) {
2929
this.$i18n.locale = lang
30-
this.$store.dispatch('setLanguage', lang)
30+
this.$store.dispatch('app/setLanguage', lang)
3131
this.$message({
3232
message: 'Switch Language Success',
3333
type: 'success'

src/components/SizeSelect/index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default {
3232
methods: {
3333
handleSetSize(size) {
3434
this.$ELEMENT.size = size
35-
this.$store.dispatch('setSize', size)
35+
this.$store.dispatch('app/setSize', size)
3636
this.refreshView()
3737
this.$message({
3838
message: 'Switch Size Success',
@@ -41,7 +41,7 @@ export default {
4141
},
4242
refreshView() {
4343
// In order to make the cached page re-rendered
44-
this.$store.dispatch('delAllCachedViews', this.$route)
44+
this.$store.dispatch('tagsView/delAllCachedViews', this.$route)
4545
4646
const { fullPath } = this.$route
4747

src/layout/Layout.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default {
4848
},
4949
methods: {
5050
handleClickOutside() {
51-
this.$store.dispatch('closeSideBar', { withoutAnimation: false })
51+
this.$store.dispatch('app/closeSideBar', { withoutAnimation: false })
5252
}
5353
}
5454
}

src/layout/components/Navbar.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ export default {
7575
},
7676
methods: {
7777
toggleSideBar() {
78-
this.$store.dispatch('toggleSideBar')
78+
this.$store.dispatch('app/toggleSideBar')
7979
},
8080
async logout() {
81-
await this.$store.dispatch('Logout')
81+
await this.$store.dispatch('user/logout')
8282
// In order to re-instantiate the vue-router object to avoid bugs
8383
location.reload()
8484
}

src/layout/components/Settings/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default {
3434
return this.$store.state.settings.tagsView
3535
},
3636
set(val) {
37-
this.$store.dispatch('changeSetting', {
37+
this.$store.dispatch('settings/changeSetting', {
3838
key: 'tagsView',
3939
value: val
4040
})

src/layout/components/TagsView/index.vue

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ export default {
106106
for (const tag of affixTags) {
107107
// Must have tag name
108108
if (tag.name) {
109-
this.$store.dispatch('addVisitedView', tag)
109+
this.$store.dispatch('tagsView/addVisitedView', tag)
110110
}
111111
}
112112
},
113113
addTags() {
114114
const { name } = this.$route
115115
if (name) {
116-
this.$store.dispatch('addView', this.$route)
116+
this.$store.dispatch('tagsView/addView', this.$route)
117117
}
118118
return false
119119
},
@@ -125,15 +125,15 @@ export default {
125125
this.$refs.scrollPane.moveToTarget(tag)
126126
// when query is different then update
127127
if (tag.to.fullPath !== this.$route.fullPath) {
128-
this.$store.dispatch('updateVisitedView', this.$route)
128+
this.$store.dispatch('tagsView/updateVisitedView', this.$route)
129129
}
130130
break
131131
}
132132
}
133133
})
134134
},
135135
refreshSelectedTag(view) {
136-
this.$store.dispatch('delCachedView', view).then(() => {
136+
this.$store.dispatch('tagsView/delCachedView', view).then(() => {
137137
const { fullPath } = view
138138
this.$nextTick(() => {
139139
this.$router.replace({
@@ -143,20 +143,20 @@ export default {
143143
})
144144
},
145145
closeSelectedTag(view) {
146-
this.$store.dispatch('delView', view).then(({ visitedViews }) => {
146+
this.$store.dispatch('tagsView/delView', view).then(({ visitedViews }) => {
147147
if (this.isActive(view)) {
148148
this.toLastView(visitedViews)
149149
}
150150
})
151151
},
152152
closeOthersTags() {
153153
this.$router.push(this.selectedTag)
154-
this.$store.dispatch('delOthersViews', this.selectedTag).then(() => {
154+
this.$store.dispatch('tagsView/delOthersViews', this.selectedTag).then(() => {
155155
this.moveToCurrentTag()
156156
})
157157
},
158158
closeAllTags(view) {
159-
this.$store.dispatch('delAllViews').then(({ visitedViews }) => {
159+
this.$store.dispatch('tagsView/delAllViews').then(({ visitedViews }) => {
160160
if (this.affixTags.some(tag => tag.path === view.path)) {
161161
return
162162
}

src/layout/mixin/ResizeHandler.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default {
77
watch: {
88
$route(route) {
99
if (this.device === 'mobile' && this.sidebar.opened) {
10-
store.dispatch('closeSideBar', { withoutAnimation: false })
10+
store.dispatch('app/closeSideBar', { withoutAnimation: false })
1111
}
1212
}
1313
},
@@ -17,8 +17,8 @@ export default {
1717
mounted() {
1818
const isMobile = this.isMobile()
1919
if (isMobile) {
20-
store.dispatch('toggleDevice', 'mobile')
21-
store.dispatch('closeSideBar', { withoutAnimation: true })
20+
store.dispatch('app/toggleDevice', 'mobile')
21+
store.dispatch('app/closeSideBar', { withoutAnimation: true })
2222
}
2323
},
2424
methods: {
@@ -29,10 +29,10 @@ export default {
2929
resizeHandler() {
3030
if (!document.hidden) {
3131
const isMobile = this.isMobile()
32-
store.dispatch('toggleDevice', isMobile ? 'mobile' : 'desktop')
32+
store.dispatch('app/toggleDevice', isMobile ? 'mobile' : 'desktop')
3333

3434
if (isMobile) {
35-
store.dispatch('closeSideBar', { withoutAnimation: true })
35+
store.dispatch('app/closeSideBar', { withoutAnimation: true })
3636
}
3737
}
3838
}

src/permission.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ router.beforeEach((to, from, next) => {
2929
if (store.getters.roles.length === 0) {
3030
// 判断当前用户是否已拉取完user_info信息
3131
store
32-
.dispatch('getInfo') // dispatch @/store/modules/user login getInfo
32+
.dispatch('user/getInfo')
3333
.then(res => {
3434
// 拉取user_info
3535
const { roles } = res // note: roles must be a object array! such as: [{id: '1', name: 'editor'}, {id: '2', name: 'developer'}]
36-
store.dispatch('generateRoutes', { roles }).then(accessRoutes => {
36+
store.dispatch('permission/generateRoutes', { roles }).then(accessRoutes => {
3737
// 根据roles权限生成可访问的路由表
3838
router.addRoutes(accessRoutes) // 动态添加可访问路由表
3939
next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
4040
})
4141
})
4242
.catch(err => {
43-
store.dispatch('resetToken').then(() => {
43+
store.dispatch('user/resetToken').then(() => {
4444
Message.error(err)
4545
next({ path: '/' })
4646
})

src/store/modules/app.js

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,64 @@
11
import Cookies from 'js-cookie'
22

3-
const app = {
4-
state: {
5-
sidebar: {
6-
opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
7-
withoutAnimation: false
8-
},
9-
device: 'desktop',
10-
language: Cookies.get('language') || 'en',
11-
size: Cookies.get('size') || 'medium'
12-
},
13-
mutations: {
14-
TOGGLE_SIDEBAR: state => {
15-
state.sidebar.opened = !state.sidebar.opened
16-
state.sidebar.withoutAnimation = false
17-
if (state.sidebar.opened) {
18-
Cookies.set('sidebarStatus', 1)
19-
} else {
20-
Cookies.set('sidebarStatus', 0)
21-
}
22-
},
23-
CLOSE_SIDEBAR: (state, withoutAnimation) => {
3+
const state = {
4+
sidebar: {
5+
opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
6+
withoutAnimation: false
7+
},
8+
device: 'desktop',
9+
language: Cookies.get('language') || 'en',
10+
size: Cookies.get('size') || 'medium'
11+
}
12+
13+
const mutations = {
14+
TOGGLE_SIDEBAR: state => {
15+
state.sidebar.opened = !state.sidebar.opened
16+
state.sidebar.withoutAnimation = false
17+
if (state.sidebar.opened) {
18+
Cookies.set('sidebarStatus', 1)
19+
} else {
2420
Cookies.set('sidebarStatus', 0)
25-
state.sidebar.opened = false
26-
state.sidebar.withoutAnimation = withoutAnimation
27-
},
28-
TOGGLE_DEVICE: (state, device) => {
29-
state.device = device
30-
},
31-
SET_LANGUAGE: (state, language) => {
32-
state.language = language
33-
Cookies.set('language', language)
34-
},
35-
SET_SIZE: (state, size) => {
36-
state.size = size
37-
Cookies.set('size', size)
3821
}
3922
},
40-
actions: {
41-
toggleSideBar({ commit }) {
42-
commit('TOGGLE_SIDEBAR')
43-
},
44-
closeSideBar({ commit }, { withoutAnimation }) {
45-
commit('CLOSE_SIDEBAR', withoutAnimation)
46-
},
47-
toggleDevice({ commit }, device) {
48-
commit('TOGGLE_DEVICE', device)
49-
},
50-
setLanguage({ commit }, language) {
51-
commit('SET_LANGUAGE', language)
52-
},
53-
setSize({ commit }, size) {
54-
commit('SET_SIZE', size)
55-
}
23+
CLOSE_SIDEBAR: (state, withoutAnimation) => {
24+
Cookies.set('sidebarStatus', 0)
25+
state.sidebar.opened = false
26+
state.sidebar.withoutAnimation = withoutAnimation
27+
},
28+
TOGGLE_DEVICE: (state, device) => {
29+
state.device = device
30+
},
31+
SET_LANGUAGE: (state, language) => {
32+
state.language = language
33+
Cookies.set('language', language)
34+
},
35+
SET_SIZE: (state, size) => {
36+
state.size = size
37+
Cookies.set('size', size)
5638
}
5739
}
5840

59-
export default app
41+
const actions = {
42+
toggleSideBar({ commit }) {
43+
commit('TOGGLE_SIDEBAR')
44+
},
45+
closeSideBar({ commit }, { withoutAnimation }) {
46+
commit('CLOSE_SIDEBAR', withoutAnimation)
47+
},
48+
toggleDevice({ commit }, device) {
49+
commit('TOGGLE_DEVICE', device)
50+
},
51+
setLanguage({ commit }, language) {
52+
commit('SET_LANGUAGE', language)
53+
},
54+
setSize({ commit }, size) {
55+
commit('SET_SIZE', size)
56+
}
57+
}
58+
59+
export default {
60+
namespaced: true,
61+
state,
62+
mutations,
63+
actions
64+
}

src/store/modules/errorLog.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
const errorLog = {
2-
state: {
3-
logs: []
4-
},
5-
mutations: {
6-
ADD_ERROR_LOG: (state, log) => {
7-
state.logs.push(log)
8-
}
9-
},
10-
actions: {
11-
addErrorLog({ commit }, log) {
12-
commit('ADD_ERROR_LOG', log)
13-
}
1+
2+
const state = {
3+
logs: []
4+
}
5+
6+
const mutations = {
7+
ADD_ERROR_LOG: (state, log) => {
8+
state.logs.push(log)
149
}
1510
}
1611

17-
export default errorLog
12+
const actions = {
13+
addErrorLog({ commit }, log) {
14+
commit('ADD_ERROR_LOG', log)
15+
}
16+
}
17+
18+
export default {
19+
namespaced: true,
20+
state,
21+
mutations,
22+
actions
23+
}

src/store/modules/permission.js

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,37 @@ export function filterAsyncRoutes(routes, roles) {
3434
return res
3535
}
3636

37-
const permission = {
38-
state: {
39-
routes: [],
40-
addRoutes: []
41-
},
42-
mutations: {
43-
SET_ROUTES: (state, routes) => {
44-
state.addRoutes = routes
45-
state.routes = constantRoutes.concat(routes)
46-
}
47-
},
48-
actions: {
49-
generateRoutes({ commit }, data) {
50-
return new Promise(resolve => {
51-
const { roles } = data
52-
let accessedRoutes
53-
if (roles.includes('admin')) {
54-
accessedRoutes = asyncRoutes
55-
} else {
56-
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
57-
}
58-
commit('SET_ROUTES', accessedRoutes)
59-
resolve(accessedRoutes)
60-
})
61-
}
37+
const state = {
38+
routes: [],
39+
addRoutes: []
40+
}
41+
42+
const mutations = {
43+
SET_ROUTES: (state, routes) => {
44+
state.addRoutes = routes
45+
state.routes = constantRoutes.concat(routes)
6246
}
6347
}
6448

65-
export default permission
49+
const actions = {
50+
generateRoutes({ commit }, data) {
51+
return new Promise(resolve => {
52+
const { roles } = data
53+
let accessedRoutes
54+
if (roles.includes('admin')) {
55+
accessedRoutes = asyncRoutes
56+
} else {
57+
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
58+
}
59+
commit('SET_ROUTES', accessedRoutes)
60+
resolve(accessedRoutes)
61+
})
62+
}
63+
}
64+
65+
export default {
66+
namespaced: true,
67+
state,
68+
mutations,
69+
actions
70+
}

0 commit comments

Comments
 (0)