store/index.js
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import reducer from './reducer.js'
const store = createStore(reducer, applyMiddleware(thunk))
export default store
Reducer
const defaultState = {
currentMenu: {}, // 当前页面菜单对象
allRoutesList: [], // 全局路由集合
baseInfo: {}
}
const reducer = (state, action) => { //就是一个方法函数
if (action.type === 'getCurrentMenu') {
let newState = { ...state } //JSON.parse(JSON.stringify(state))
newState.currentMenu = { ...action.value}
return newState
}
if (action.type === 'getAllRoutesList') {
let newState = { ...state }
newState.allRoutesList = [...action.value]
return newState
}
return state
}
export default reducer
ActionType
export const GETCURRENTMENU = 'getCurrentMenu';
export const GETALLROUTESLIST = 'getAllRoutesList';
Action
import {
GETCURRENTMENU,
GETALLROUTESLIST
} from './actionType'
export const getCurrentMenu = (value) => ({
type: GETCURRENTMENU,
value
});
export const getAllRoutesList = (value) => ({
type: GETALLROUTESLIST,
value
});
写入
import store from './store/index.js'
store.dispatch(getCurrentMenu(_nowMenu));
store.dispatch(getAllRoutesList(routesList));
取用
import store from '@/store'
let cm = store.getState().currentMenu ? store.getState().currentMenu : {};