简体   繁体   English

Nuxtjs Vuex不保存更改

[英]Nuxtjs Vuex not saving changes

I have data between sessions that is saved in window.localStorage .我有保存在window.localStorage会话之间的数据。 When a new session starts a plugin will grab the data and add it to the store.当新会话开始时,插件将获取数据并将其添加到存储中。

// ./store/data.js

export const state = () => ({
  data: []
})

export const mutations = {
  addItemToData (state, item) {
    state.data = state.data.push(item)
  },
  setData (state, data) {
    state.data = data
  },
}

// ./store/index.js

import localStorage from '../plugins/localStorage'

export const plugins = [localStorage]

// plugins/localStorage.js

const localStorage = store => {

  store.subscribe((mutation, state) => {

    if (mutation.type === 'data/addItemToData') {
      console.log('saving added item to storage')
      window.localStorage.setItem('data', JSON.stringify(state.data.data))
    }

  })

  // called when the store is initialized
  if (typeof window !== 'undefined') {
    if (window.localStorage.data) {
      store.commit('data/setData', JSON.parse(window.localStorage.getItem('data')))
    }
  }
}

export default localStorage

I've thrown all sorts of console statements in, and they all seem to output what they should.我已经抛出了各种各样的控制台语句,它们似乎都输出了它们应该输出的内容。 The data is in the localStorage, the mutations are firing, but after all that the data isn't in the store.数据在 localStorage 中,突变正在触发,但毕竟数据不在存储中。

Any help would be great, thanks!任何帮助都会很棒,谢谢!

several things that do not make sense and that you can resolve一些没有意义的事情,你可以解决

#1 #1

addItemToData (state, item) {
  state.data = state.data.push(item)
}

Array.push() does not return anything, so it should be written as Array.push()不返回任何东西,所以应该写成

addItemToData (state, item) {
  state.data.push(item)
}

#2 #2

in your localStorage.js file, when you initialize the data storage, you are assuming that a data variable exists:在您的localStorage.js文件中,当您初始化数据存储时,您假设存在一个data变量:

if (window.localStorage.data) { ...

but that will never exist from the code you show, as you are adding the data to another variable但这永远不会从您显示的代码中存在,因为您将数据添加到另一个变量

window.localStorage.setItem('cart', ...

either change cart into data or data into cartcart更改为data或将data更改为cart

#3 #3

If still does not work, I would suspect that your plugin is running before the store is actually initialized, for that, make sure you wait a moment before attempt to fill the store with the localStorage data如果仍然不起作用,我会怀疑您的插件在store实际初始化之前正在运行,为此,请确保在尝试使用 localStorage 数据填充商店之前稍等片刻

something like就像是

   ...
   if (window.localStorage.data) {
     setTimeout(() => {
       store.commit('data/setData', JSON.parse(window.localStorage.getItem('data')))
     }, 500)
   }

Working example is available on Netlify and code on GitHub Netlify上提供了工作示例, GitHub 上提供了代码

您可以使用 vuex-persistedstate 保存在本地存储中

Array.push Returns the length of the Array after insertion, In your case, it overrides the data Array to Number when you make a mutation. Array.push插入后返回 Array 的length ,在您的情况下,它会在您进行更改时将data Array 覆盖为Number Do like the following..喜欢下面的..

export const state = () => ({
  data: []
})

export const mutations = {
  addItemToData (state, item) {
    state.data.push(item)
    // OR 
    state.data = [...state.data, item]
  },
  setData (state, data) {
    state.data = data
  },
}

And also make sure, you have data in localStorage.还要确保你在 localStorage 中有数据。

 try { // handles if any parsing errors
   const data = JSON.parse(window.localStorage.getItem('data')
   store.commit('data/setData', data)
 } catch(err) {
   console.log('JSON parsing error', err)
 }
 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM