From 29d28c3231af44e5cc723a442691a7194915c7b1 Mon Sep 17 00:00:00 2001 From: Pan Date: Thu, 13 Jul 2017 16:54:54 +0800 Subject: [PATCH] fix bug in vuex of strict model --- src/store/modules/app.js | 13 ++++++++++--- src/store/modules/permission.js | 5 +++-- src/utils/index.js | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/store/modules/app.js b/src/store/modules/app.js index e30ae3bf..4b612ec2 100644 --- a/src/store/modules/app.js +++ b/src/store/modules/app.js @@ -1,5 +1,6 @@ import Cookies from 'js-cookie'; + const app = { state: { sidebar: { @@ -19,11 +20,17 @@ const app = { state.sidebar.opened = !state.sidebar.opened; }, ADD_VISITED_VIEWS: (state, view) => { - if (state.visitedViews.includes(view)) return - state.visitedViews.push(view) + if (state.visitedViews.some(v => v.path === view.path)) return + state.visitedViews.push({ name: view.name, path: view.path }) }, DEL_VISITED_VIEWS: (state, view) => { - const index = state.visitedViews.indexOf(view) + let index + for (const [i, v] of state.visitedViews.entries()) { + if (v.path === view.path) { + index = i + break + } + } state.visitedViews.splice(index, 1) } }, diff --git a/src/store/modules/permission.js b/src/store/modules/permission.js index c4cfa4f0..dbbc9f4e 100644 --- a/src/store/modules/permission.js +++ b/src/store/modules/permission.js @@ -1,4 +1,5 @@ import { asyncRouterMap, constantRouterMap } from 'src/router'; +import { deepClone } from 'utils' /** * 通过meta.role判断是否与当前用户权限匹配 @@ -38,8 +39,8 @@ const permission = { }, mutations: { SET_ROUTERS: (state, routers) => { - state.addRouters = routers; - state.routers = constantRouterMap.concat(routers); + state.addRouters = deepClone(routers) + state.routers = deepClone(constantRouterMap.concat(routers)) } }, actions: { diff --git a/src/utils/index.js b/src/utils/index.js index 5cf482dc..3e60916f 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -250,3 +250,21 @@ }; } + + export function deepClone(source) { + if (!source && typeof source !== 'object') { + throw new Error('error arguments', 'shallowClone'); + } + const targetObj = source.constructor === Array ? [] : {}; + for (const keys in source) { + if (source.hasOwnProperty(keys)) { + if (source[keys] && typeof source[keys] === 'object') { + targetObj[keys] = source[keys].constructor === Array ? [] : {}; + targetObj[keys] = deepClone(source[keys]); + } else { + targetObj[keys] = source[keys]; + } + } + } + return targetObj; + }