刷新頁(yè)面,vuex里面數(shù)據(jù)丟失、清空。有時(shí)候我們需要把一些數(shù)據(jù)固話到本地,即使刷新也不能清空,例如:登陸狀態(tài)、token等。這是就需要用到vuex數(shù)據(jù)持久化
//需要先下載插件npm install vuex-persistedstate--save 或者 使用yarnaddvuex-persistedstate--save
//在vuex初始化時(shí)導(dǎo)入插件importpersistfrom'vuex-persistedstate'//并使用exportdefaultnewVuex.Store({state:{},mutations:{},actions:{},modules:{},plugins:[newpersist({storage:window.localStorage,}),],//會(huì)自動(dòng)保存狀態(tài),刷新時(shí)不會(huì)丟失})