const trainingList = { namespaced: true, state: { index: 0, // 当前选择实训索引 trainingList: [] // 实训初始列表 }, getters: { trainingList: (state) => { return state.trainingList; }, prevTraining: (state) => () => { if (state.trainingList && state.trainingList.length > 0) { state.index = state.index - 1; if (state.index >= 0) { return state.trainingList[state.index]; } } }, nextTraining: (state) => () => { if (state.trainingList && state.trainingList.length > 0) { state.index = state.index + 1; if (state.index < state.trainingList.length) { return state.trainingList[state.index]; } } } }, mutations: { setIndex: (state, index) => { state.index = index; }, setTrainingList: (state, trainingList) => { if (trainingList && trainingList.length) { state.trainingList = trainingList; } else { state.trainingList = []; } } }, actions: { setIndex({ commit }, index) { commit('setIndex', index); }, setIndexByTrainingObj({ commit, state }, trainingId) { let index = 0; if (state.trainingList && state.trainingList.length) { state.trainingList.forEach((elem, i) => { if (elem.id === trainingId) { index = i; } }); } commit('setIndex', index); }, setTrainingList({ commit }, trainingList) { commit('setTrainingList', trainingList); }, clearTrainingList({ commit }) { commit('setTrainingList', []); commit('setIndex', 0); } } }; export default trainingList;