import { createStore } from 'vuex'

import createPersistedstate from 'vuex-persistedstate'

export default createStore({
  state: {
    searchVal:'',
    navList: [],
    routerList: []
  },
  getters: {
    navList: state=>state.navList,
    routerList: state=>state.routerList,
  },
  mutations: {
    changeSearchVal(state,val){
      state.searchVal = val
    },
    changeNavList(state, val) {
      let data = []
      const getData = (tree) =>{
        if (tree) {
          for (let i = 0; i < tree.length; i++) {
            const item = tree[i];
            if (item.children && item.children.length) {
              getData(item.children)
            }else {
              data.push(item)
            }
          }
        }
      }
      getData(val)
      state.routerList = val
      state.navList = data
    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [
  //默认是存储在localStorage中
    createPersistedstate({
  // key: 存储数据的键名
    key: 'longStore',
  //paths:存储state中的那些数据
    paths: ['searchVal', 'navList', "routerList"]
  })]
})
