1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| import vue from 'vue' import Vuex from 'vuex' import router,{resetRouter} from '../router' import createPersistedState from 'vuex-persistedstate' vue.use(Vuex);
function addNewRoute(menuList) { console.log("打印menuList"); console.log(menuList); let routes = router.options.routes; // console.log("路由"); // console.log(routes); routes.forEach(routeItem=>{ if (routeItem.path=="/Index"){ menuList.forEach(menu=>{ let childRoute = { path:'/'+menu.menuclick, name:menu.menuname, meta:{ title:menu.menuname, }, component:()=>import('../components/'+menu.menucomponent), }; routeItem.children.push(childRoute); }); } }); resetRouter(); router.addRoutes(routes) }
export default new Vuex.Store({ state:{ menu: [] }, mutations:{ setMenu(state,menuList) { state.menu =menuList; addNewRoute(menuList); }, setRoute(state,menuList) { state.menu =menuList; addNewRoute(menuList); } }, getters:{ getMenu(state){ return state.menu } }, plugins:[createPersistedState()] });
|