vuex结合localStoarge实现对数据的存储


import Vue from 'vue';

const state = {
title: []
};

export const getters = {
title(state){
if(state.title.length==0){
state.title=JSON.parse(localStorage.getItem('title'))
}
return state.title;
}
};

export const actions = {
getTitle(commit){
Vue.http.get(‘api’,data).then(function(res){
commit('get_title',name)
})
}
};

export const mutations = {
get_title(state,name){
state.title = name;
localStorage.setItem('title',JSON.stringify(state.title))
}
};

export default {
state,
getters,
actions,
mutations
}
原文地址:https://www.cnblogs.com/lonelyGentleman/p/6730607.html