const state = () => ({ visitedViews: [], }) const mutations = { ADD_VISITED_VIEW: (state, view) => { const title = view.matched.slice(-1)[0]?.instances?.default?.$metaInfo?.title if (title) { const sameIndex = state.visitedViews.findIndex( (v) => v.name === view.name ) if (sameIndex !== -1) { if (state.visitedViews[sameIndex].fullPath !== view.fullPath) { state.visitedViews.splice(sameIndex, 1, { ...view, title }) } } else { state.visitedViews.push( Object.assign({}, view, { title, }) ) } } }, DEL_VISITED_VIEW: (state, view) => { for (const [i, v] of state.visitedViews.entries()) { if (v.name === view.name) { state.visitedViews.splice(i, 1) break } } }, DEL_OTHERS_VISITED_VIEWS: (state, view) => { state.visitedViews = state.visitedViews.filter((v) => { return v.name === view.name }) }, DEL_RIGHT_VISITED_VIEWS: (state, view) => { const selectedIndex = state.visitedViews.findIndex( (item) => item.name === view.name ) if (selectedIndex !== -1) { state.visitedViews.splice(selectedIndex + 1) } }, } const actions = { addVisitedView({ commit }, view) { commit('ADD_VISITED_VIEW', view) }, delVisitedView({ commit, state }, view) { return new Promise((resolve) => { commit('DEL_VISITED_VIEW', view) resolve([...state.visitedViews]) }) }, delOthersVisitedViews({ commit, state }, view) { return new Promise((resolve) => { commit('DEL_OTHERS_VISITED_VIEWS', view) resolve([...state.visitedViews]) }) }, delRightVisitedViews({ commit }, view) { return new Promise((resolve) => { commit('DEL_RIGHT_VISITED_VIEWS', view) resolve([...state.visitedViews]) }) }, } export default { namespaced: true, state, mutations, actions, }