当网络断开时,如果 Vuex store 被重置,mapMutations 映射将不存在


报错:出现this.set_exampleis not a function
原因:因为在重启网络时,你的 Vuex store 被重置了,导致 mapMutations 映射不存在,可以通过重新创建映射。

解决:出现假设你的 mutations.js 文件中有一个名为"set_example"的 mutation,它会重置 Vuex store。在重置 Vuex store 后,如下更改:
import {mapState,mapMutations} from 'vuex'
export default {
  data(){
    return{
    }
  },
  created(){
    window.addEventListener('offline', this.registerMutations)
  },
  destroyed() {
    window.removeEventListener('offline', this.registerMutations)
  },
  methods:{
    ...mapMutations(['set_newlist','set_oldList']),
    registerMutations() {
      this.$store.registerModule('mutations', {
        mutations: {
          set_example(state,param){
            state.example=param
          },
        }
      })
    },
  }
}

总结:使用了 created 和 destroyed 生命周期钩子来添加和删除一个事件监听器。当网络断开时,事件监听器会触发 registerMutations 方法,该方法会重新注册 mutations,并重新映射它们。这样就可以更新 Vuex store 了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值