Vuex 全状态赋值

Vuex whole state assign

我正在使用 Nuxt 和 Vuex,我正在尝试将整个命名空间状态分配给某个值,可以分配整个状态吗?还有另一种方法可以拥有命名空间模块,但将单个值分配给命名空间状态?

我正在谈论的突变的一些例子

const mutations = {
  [SET_LOCATION](state, location) {
    state = { ...location }
  }
}

const mutations = {
  [SET_CURRENCY](state, currency) {
    switch(currency) {
      case 'MXN':
      case 'EUR':
        state = currency
        break
      default: 
        state = 'USD'
    }
  }
}

但是,当这些突变被执行时,它们没有赋值,我一直在尝试赋值,但只有当我为命名空间的 属性 赋值时它才有效-state.

but it only works when I assign a new value to a property of the namespaced-state

这是有原因的。 Vue 有反应性警告。 来自 Docs

Vue cannot detect property addition or deletion. Since Vue performs the getter/setter conversion process during instance initialization, a property must be present in the data object in order for Vue to convert it and make it reactive.

为避免此问题,请确保在初始化商店时在“state”对象中声明所有键

例如:


state = {locationState:null}
// Make the store with the state
const mutations = {
  [SET_LOCATION](state, location) {
    state.locationState = location 
  }
}

让我知道这是否适合你。