const KEY = 'VUEX_STORE'
import Cookies from "js-cookie";
export default function(store){
    //保存仓库数据到本地
    window.addEventListener('beforeunload',(e)=>{
        if(Cookies.get('token')){
            localStorage.setItem(KEY, JSON.stringify(store.state))
        }
    })
    window.addEventListener("popstate", ()=>{
        if(Cookies.get('token')){
            localStorage.setItem(KEY, JSON.stringify(store.state))
        }
    });
    
    //恢复仓库数据
    try{
       const localStorage = localStorage.getItem(KEY)
        if(localStorage){
            store.replaceState(JSON.parse(localStorage.getItem(KEY)))
        } 
    }
    catch{
        
    }
    
}