Skip to content

Commit

Permalink
简化vuex中多余的permission数据
Browse files Browse the repository at this point in the history
因扁平化后的permission数据只在router中使用,故在vuex中删除了该数据,改用变量代替。

建议将有必要的数据都放在每个路由的meta信息中,以保证在今后页面跳转时能够获取到该信息
  • Loading branch information
harsima committed Apr 18, 2018
1 parent 3098378 commit 355e8c1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
7 changes: 5 additions & 2 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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
Expand Down
11 changes: 2 additions & 9 deletions src/store/modules/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@ import Auth from '@/util/auth'

const state = {
token: '',
navList: [],
permissionList: []
navList: []
}

const mutations = {
setNavList: (state, data) => {
state.navList = data
},

setPermissionList: (state, data) => {
state.permissionList = data
},

setToken: (state, data) => {
if(data){
Auth.setToken(data)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -110,7 +104,7 @@ const actions = {
},

// 将菜单列表扁平化形成权限列表
getPermissionList({commit, state}){
getPermissionList({state}){
return new Promise((resolve) =>{
let permissionList = []
// 将菜单数据扁平化为一级
Expand All @@ -124,7 +118,6 @@ const actions = {
}
}
flatNavList(state.navList)
commit("setPermissionList", permissionList)
resolve(permissionList)
})
}
Expand Down

0 comments on commit 355e8c1

Please sign in to comment.