fix bug in vuex of strict model

This commit is contained in:
Pan 2017-07-13 16:54:54 +08:00
parent 62cb24c1a6
commit 29d28c3231
3 changed files with 31 additions and 5 deletions

View file

@ -1,5 +1,6 @@
import Cookies from 'js-cookie'; import Cookies from 'js-cookie';
const app = { const app = {
state: { state: {
sidebar: { sidebar: {
@ -19,11 +20,17 @@ const app = {
state.sidebar.opened = !state.sidebar.opened; state.sidebar.opened = !state.sidebar.opened;
}, },
ADD_VISITED_VIEWS: (state, view) => { ADD_VISITED_VIEWS: (state, view) => {
if (state.visitedViews.includes(view)) return if (state.visitedViews.some(v => v.path === view.path)) return
state.visitedViews.push(view) state.visitedViews.push({ name: view.name, path: view.path })
}, },
DEL_VISITED_VIEWS: (state, view) => { 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) state.visitedViews.splice(index, 1)
} }
}, },

View file

@ -1,4 +1,5 @@
import { asyncRouterMap, constantRouterMap } from 'src/router'; import { asyncRouterMap, constantRouterMap } from 'src/router';
import { deepClone } from 'utils'
/** /**
* 通过meta.role判断是否与当前用户权限匹配 * 通过meta.role判断是否与当前用户权限匹配
@ -38,8 +39,8 @@ const permission = {
}, },
mutations: { mutations: {
SET_ROUTERS: (state, routers) => { SET_ROUTERS: (state, routers) => {
state.addRouters = routers; state.addRouters = deepClone(routers)
state.routers = constantRouterMap.concat(routers); state.routers = deepClone(constantRouterMap.concat(routers))
} }
}, },
actions: { actions: {

View file

@ -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;
}