1. 程式人生 > >解決vuex儲存複雜引數(如物件陣列等)重新整理資料丟失問題

解決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]”