setTabs.js 1.1 KB
import util from '@/libs/util.js'

const getDefaultState = () => {
  return {
    openTab: [], //所有打开的路由
    activeIndex: '/panel' //激活状态
  }
}

const state = getDefaultState()

const mutations = {
  // 添加tabs
  ADD_TABS (state, data) {
    state.openTab.push(data);
  },
  // 删除tabs
  DELETE_TABS (state, route) {
    let index = 0;
    for (let option of state.openTab) {
      if (option.route === route) {
        break;
      }
      index++;
    }
    state.openTab.splice(index, 1);
  },
  // 设置当前激活的tab
  SET_ACTIVE_INDEX (state, index) {
    state.activeIndex = index;
  },
  // 初始化
  INIT_TABS(state) {
    state.openTab = [];
    state.openTab[0] = { route: "/panel", name: "首页" }
  },
}

const actions = {
  add_tabs({ commit }, data) {
    commit('ADD_TABS', data)
  },
  delete_tabs({ commit }, route) {
    commit('DELETE_TABS', route)
  },
  set_active_index({ commit }, index) {
    commit('SET_ACTIVE_INDEX', index)
  },
  init_tabs({ commit }) {
    commit('INIT_TABS')
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}