解决vuex存储复杂参数(如对象数组等)刷新数据丢失问题

 

我需要在搜索页拿到结果之后跳转到搜索结果页并携带搜索结果

尝试过几种方法之后最终采用vuex+sessionStorage结合的方法在mutations中

setResultValue(state,flag){
  sessionStorage.setItem("resultValue", JSON.stringify(flag))
  state.resultValue = flag
}

在getters中

getResultValue

getResultValue(state){
  state.resultValue = sessionStorage.getItem("resultValue")
  return state.resultValue
}

在跳转后的页面获取这个数据

this.resultValue = JSON.parse(store.getters.getResultValue)

这里可以看到我们用了JSON.stringify和JSON.parse是因为sessionStorage存储对象的需要,不然在页面获取时只能得到:“[object,object]”

 

猜你喜欢

转载自blog.csdn.net/liona_koukou/article/details/83829363