From 355e8c1863f0dae9f555dc4332f0db7cf512b345 Mon Sep 17 00:00:00 2001 From: harsima Date: Wed, 18 Apr 2018 20:14:57 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96vuex=E4=B8=AD=E5=A4=9A?= =?UTF-8?q?=E4=BD=99=E7=9A=84permission=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 因扁平化后的permission数据只在router中使用,故在vuex中删除了该数据,改用变量代替。 建议将有必要的数据都放在每个路由的meta信息中,以保证在今后页面跳转时能够获取到该信息 --- src/router/index.js | 7 +++++-- src/store/modules/auth/index.js | 11 ++--------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index 8e442bc..dcce41b 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -8,13 +8,16 @@ import store from '../store' import staticRoute from './staticRoute' import whiteList from './whiteList' +var permissionList = [] + function initRoute(router){ return new Promise((resolve) => { - if(store.state.auth.permissionList.length == 0){ + if(permissionList.length == 0){ console.log("没有权限数据,正在获取") store.dispatch('auth/getNavList').then(() => { store.dispatch('auth/getPermissionList').then((res) => { console.log("权限列表生成完毕") + permissionList = res res.forEach(function(v){ let routeItem = router.match(v.path) if(routeItem){ @@ -61,7 +64,7 @@ router.beforeEach((to, from, next) => { initRoute(router).then(() => { let isPermission = false console.log("进入权限判断") - store.state.auth.permissionList.forEach((v) => { + permissionList.forEach((v) => { // 判断跳转的页面是否在权限列表中 if(v.path == to.fullPath){ isPermission = true diff --git a/src/store/modules/auth/index.js b/src/store/modules/auth/index.js index 2243062..f74c40d 100644 --- a/src/store/modules/auth/index.js +++ b/src/store/modules/auth/index.js @@ -4,8 +4,7 @@ import Auth from '@/util/auth' const state = { token: '', - navList: [], - permissionList: [] + navList: [] } const mutations = { @@ -13,10 +12,6 @@ const mutations = { state.navList = data }, - setPermissionList: (state, data) => { - state.permissionList = data - }, - setToken: (state, data) => { if(data){ Auth.setToken(data) @@ -53,7 +48,6 @@ const actions = { logout({commit}) { return new Promise((resolve) => { commit('setToken', '') - commit('setPermissionList', []) commit('user/setName', '', { root: true }) commit('tagNav/removeTagNav', '', {root: true}) resolve() @@ -110,7 +104,7 @@ const actions = { }, // 将菜单列表扁平化形成权限列表 - getPermissionList({commit, state}){ + getPermissionList({state}){ return new Promise((resolve) =>{ let permissionList = [] // 将菜单数据扁平化为一级 @@ -124,7 +118,6 @@ const actions = { } } flatNavList(state.navList) - commit("setPermissionList", permissionList) resolve(permissionList) }) }