页面刷新后,Vuex状态在突变中为空,而不是预定义的对象

时间:2019-05-24 11:49:46

标签: javascript vue.js vuex

在一个简单的应用程序中,我创建了一组与组件创建挂钩相关的简单突变和动作。在浏览器中,按F5键后,在Vuex选项卡上打开vue-devtools 时,我会在应用程序启动时收到错误消息,尽管这种情况不会发生。

主要问题:为什么状态为“ null”以及如何更改它?

store.js

export default new Vuex.Store({
  state: {
    a: undefined,
    b: undefined
  },
  mutations: {
    SET_A (state, a) {
      console.info(a)
      state.a = a // ← store.js?c0d6:14
    },
    SET_B (state, b) {
      state.b = b
    }
  },
  actions: {
    setA ({ commit }, a) {
      console.info(a)
      commit('SET_A', a)
    },
    setB ({ commit }, b) {
      commit('SET_B', b)
    }
  }
})

Home.vue

<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'

export default {
  name: 'home',
  components: {
    HelloWorld
  },
  created: function () {
    this.$store.dispatch('setA', 'A')
  }
}
</script>

About.vue

<script>
export default {
  name: 'home',
  components: {
  },
  created: function () {
    this.$store.dispatch('setB', 'B')
  }
}
</script>

在“组件”选项卡上控制台登录

log.js?1afd:24 [HMR] Waiting for update signal from WDS...
store.js?c0d6:22 A
store.js?c0d6:13 A
backend.js:1585  vue-devtools  Detected Vue v2.6.10 

在Vuex选项卡上控制台登录

log.js?1afd:24 [HMR] Waiting for update signal from WDS...
store.js?c0d6:22 A
store.js?c0d6:13 A
backend.js:1585  vue-devtools  Detected Vue v2.6.10 

store.js?c0d6:13 null

backend.js:14674 Uncaught TypeError: Cannot set property 'a' of null
    at Store.SET_A (store.js?c0d6:14)
    at wrappedMutationHandler (vuex.esm.js?2ba1:725)
    at backend.js:14664
    at Array.forEach (<anonymous>)
    at VuexBackend.replayMutations (backend.js:14664)
    at VuexBackend.onInspectState (backend.js:14355)
    at Bridge.emit (backend.js:5472)
    at Bridge._emit (backend.js:5172)
    at backend.js:5097
    at Array.forEach (<anonymous>)
SET_A @ store.js?c0d6:14
wrappedMutationHandler @ vuex.esm.js?2ba1:725
(anonymous) @ backend.js:14664
replayMutations @ backend.js:14664
onInspectState @ backend.js:14355
emit @ backend.js:5472
_emit @ backend.js:5172
(anonymous) @ backend.js:5097
(anonymous) @ backend.js:5097
listener @ backend.js:2568
postMessage (async)
o @ proxy.js:1

store.js?c0d6:13 null

backend.js:14674 Uncaught TypeError: Cannot set property 'a' of null
    at Store.SET_A (store.js?c0d6:14)
    at wrappedMutationHandler (vuex.esm.js?2ba1:725)
    at backend.js:14664
    at Array.forEach (<anonymous>)
    at VuexBackend.replayMutations (backend.js:14664)
    at VuexBackend.onInspectState (backend.js:14355)
    at Bridge.emit (backend.js:5472)
    at Bridge._emit (backend.js:5172)
    at backend.js:5097
    at Array.forEach (<anonymous>)
SET_A @ store.js?c0d6:14
wrappedMutationHandler @ vuex.esm.js?2ba1:725
(anonymous) @ backend.js:14664
replayMutations @ backend.js:14664
onInspectState @ backend.js:14355
emit @ backend.js:5472
_emit @ backend.js:5172
(anonymous) @ backend.js:5097
(anonymous) @ backend.js:5097
listener @ backend.js:2568
postMessage (async)
o @ proxy.js:1

1 个答案:

答案 0 :(得分:0)

在devtools设置中是“ New Vuex后端”。

我不知道要抓什么,但是禁用此选项可以解决我的问题。