Skip to content

Commit 9aaebc4

Browse files
author
Riley Ren
committed
optimize active menu item logic
1 parent 3b6ac85 commit 9aaebc4

File tree

5 files changed

+79
-44
lines changed

5 files changed

+79
-44
lines changed

src/App.vue

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div id="app">
33
<div class="wrapper">
44
<div class="wrapper-header">
5-
<Header :active-key="activeKey" @on-change="handleNavMenuChange" @on-sign-out="signOut" @on-sign-in="showSignIn"></Header>
5+
<Header @on-change="handleNavMenuChange" @on-sign-out="signOut" @on-sign-in="showSignIn"></Header>
66
</div>
77
<div class="wrapper-container">
88
<div class="wrapper-content ivu-article">
@@ -32,14 +32,13 @@ export default {
3232
},
3333
data() {
3434
return {
35-
activeKey: 'projects',
36-
signIn: false
35+
// activeKey: 'projects',
3736
};
3837
},
3938
computed: {
4039
...mapGetters('sign', {
4140
'showSignModal': 'showModal'
42-
})
41+
}),
4342
},
4443
methods: {
4544
...mapMutations('sign', {
@@ -53,7 +52,7 @@ export default {
5352
});
5453
},
5554
handleNavMenuChange (val) {
56-
this.activeKey = val;
55+
this.activeKey = val;
5756
}
5857
},
5958
};

src/pages/components/Header.vue

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<Menu mode="horizontal" :active-name="currentActiveKey" @on-select="handleSelect">
2+
<Menu mode="horizontal" :active-name="activeMenu" @on-select="handleSelect">
33
<div class="wrapper-header-nav">
44
<router-link to="/" class="wrapper-header-nav-logo">
55
<span>营销平台</span>
@@ -48,70 +48,69 @@
4848

4949
<script>
5050
import { mapGetters, mapMutations } from 'vuex';
51+
import * as types from '@/store/mutation-types';
5152
5253
export default {
5354
props: {
54-
activeKey: String
5555
},
5656
data () {
5757
return {
58-
currentActiveKey: this.activeKey,
5958
};
6059
},
60+
mounted() {
61+
var menu = '';
62+
switch(this.$route.path) {
63+
case '/template':
64+
menu = 'template';
65+
break;
66+
case '/project':
67+
menu = 'project';
68+
break;
69+
case '/charge-off':
70+
menu = 'charge-off';
71+
break;
72+
}
73+
this.changeActiveMenu({ menu });
74+
},
6175
computed: {
6276
...mapGetters('sign', {
63-
'signed': 'signed'
64-
})
77+
'signed': 'signed',
78+
}),
79+
...mapGetters('app', [
80+
'activeMenu',
81+
]),
6582
},
6683
watch: {
67-
activeKey (val) {
68-
this.currentActiveKey = val;
69-
},
70-
currentActiveKey (val) {
71-
this.$emit('on-change', val);
72-
}
7384
},
7485
methods: {
86+
...mapMutations('app', {
87+
changeActiveMenu: types.ACTIVE_MENU_CHANGE,
88+
}),
7589
handleSelect (type) {
90+
let menu = '';
7691
if (type === 'index') {
7792
this.$router.push('/');
7893
} else if (type === 'project') {
7994
this.$router.push('/project');
95+
menu = 'project';
8096
} else if (type === 'template') {
8197
this.$router.push('/template');
98+
menu = 'template';
8299
} else if (type === 'charge-off') {
83100
this.$router.push('/charge-off');
101+
menu = 'charge-off';
84102
} else if (type === 'sign-out') {
85103
this.$emit('on-sign-out');
86104
}
87-
this.$nextTick(() => {
88-
this.updateActiveNav();
89-
});
90-
},
91-
updateActiveNav () {
92-
// const componentList = [
93-
// '/docs/guide/install',
94-
// '/docs/guide/start',
95-
// '/docs/guide/i18n',
96-
// '/docs/guide/theme',
97-
// '/docs/guide/iview-loader',
98-
// '/overview',
99-
// '/docs/guide/update'
100-
// ];
101-
102-
const route = this.$route.path;
103-
104-
105-
// if (route.indexOf('component') > -1 || componentList.indexOf(route) > -1) {
106-
// this.currentActiveKey = 'component';
107-
// } else if (route.indexOf('practice') > -1) {
108-
// this.currentActiveKey = 'practice';
109-
// } else if (route.indexOf('live') > -1) {
110-
// this.currentActiveKey = 'live';
111-
// } else {
112-
// this.currentActiveKey = 'guide';
113-
// }
105+
this.changeActiveMenu({ menu });
106+
// this.$nextTick(() => {
107+
// this.updateActiveNav();
108+
// });
114109
},
110+
// updateActiveNav () {
111+
// const route = this.$route.path;
112+
// console.log(route);
113+
// },
115114
handleSignIn () {
116115
this.$emit('on-sign-in');
117116
}

src/store/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as actions from './actions';
55
import * as getters from './getters';
66
import todo from './modules/todo';
77
import sign from './modules/sign';
8+
import app from './modules/app';
89

910
Vue.use(Vuex);
1011

@@ -15,7 +16,8 @@ export default new Vuex.Store({
1516
getters,
1617
modules: {
1718
todo,
18-
sign
19+
sign,
20+
app,
1921
},
2022
strict: debug,
2123
plugins: debug ? [createLogger()] : [],

src/store/modules/app.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as types from '../mutation-types';
2+
3+
/* eslint-disable no-shadow */
4+
5+
// initial state
6+
const state = {
7+
activeMenu: '',
8+
};
9+
10+
// getters
11+
const getters = {
12+
activeMenu: (state) => state.activeMenu,
13+
};
14+
15+
// actions
16+
const actions = {
17+
};
18+
19+
// mutations
20+
const mutations = {
21+
[types.ACTIVE_MENU_CHANGE](state, { menu }) {
22+
state.activeMenu = menu;
23+
}
24+
};
25+
26+
export default {
27+
state,
28+
getters,
29+
actions,
30+
mutations,
31+
namespaced: true,
32+
};

src/store/mutation-types.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ export const SIGN_OUT = 'SIGN_OUT';
1010
export const RESTORE_SIGN = 'RESTORE_SIGN';
1111
export const SHOW_SIGN_IN_MODAL = 'SHOW_SIGN_IN_MODAL';
1212
export const HIDE_SIGN_IN_MODAL = 'HIDE_SIGN_IN_MODAL';
13+
14+
15+
export const ACTIVE_MENU_CHANGE = 'ACTIVE_MENU_CHANGE';

0 commit comments

Comments
 (0)